private string GetFetchTokenParam(JDClientInfo jdClientInfo) { string now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); string pwd = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(jdClientInfo.UserPwd, "MD5") .ToLower(); string sign = $"{jdClientInfo.ClientSecret}{now}{jdClientInfo.ClientId}{jdClientInfo.UserName}{pwd}access_token{jdClientInfo.ClientSecret}"; sign = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sign, "MD5") .ToUpper(); string param = $"grant_type=access_token&client_id={jdClientInfo.ClientId}&client_secret={jdClientInfo.ClientSecret}×tamp={now}&username={jdClientInfo.UserName}&password={pwd}&sign={sign}"; return(param); }
public JDToken GetJDToken(JDClientInfo jdClientInfo) { _jdTokens.TryGetValue(jdClientInfo.ClientId, out JDToken token); if (token == null || token.Access_Token_Expires <= DateTime.Now) { string str = CallApi("https://bizapi.jd.com/oauth2/accessToken", GetFetchTokenParam(jdClientInfo)); JDTokenResult json = JsonConvert.DeserializeObject <JDTokenResult>(str); if (json != null && json.success) { token = json.ToJdToken(); _jdTokens.Remove(jdClientInfo.ClientId); _jdTokens.Add(jdClientInfo.ClientId, token); } else { _log.InsertLog(LogLevel.Error, "京东-获取Token失败", str); } } //快过期刷新Token if (token != null && (token.Access_Token_Expires - DateTime.Now).TotalHours <= 2 && (token.Refresh_Token_Expires - DateTime.Now).TotalHours > 1 ) { token = this.RefreshToken(token.Refresh_Token, jdClientInfo.ClientId, jdClientInfo.ClientSecret); _jdTokens.Remove(jdClientInfo.ClientId); _jdTokens.Add(jdClientInfo.ClientId, token); } if (token == null) { _jdTokens.Remove(jdClientInfo.ClientId); } return(token); }