public JDToken RefreshToken(string refreshToken, string clientId, string clientSecret) { JDToken result = null; string str = CallApi("https://bizapi.jd.com/oauth2/accessToken", $"refresh_token={refreshToken}&client_id={clientId}&client_secret={clientSecret}"); JDTokenResult json = JsonConvert.DeserializeObject <JDTokenResult>(str); if (json != null && json.success) { result = json.ToJdToken(); } else { _log.InsertLog(LogLevel.Error, "京东-刷新Token失败", str); } return(result); }
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); }