/// <summary> /// 通过refresh_token刷新或续期access_token。 /// </summary> /// <param name="refresh_token"></param> /// <returns></returns> public static AccessTokenModel RefreshToken(string refresh_token) { if (string.IsNullOrEmpty(refresh_token)) { throw new Exception("refresh_token 为必填参数!"); } string url = string.Format("https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={0}&grant_type=refresh_token&refresh_token={1}", Privacy.AppId, refresh_token); string result = HttpRequestHelper.Request(url); PageOAuth_AccessToken model = JsonHelper.DeSerialize <PageOAuth_AccessToken>(result); if (model == null || string.IsNullOrEmpty(model.openid)) { StringBuilder msg = new StringBuilder("刷新access_token有效期失败,url=" + url); msg.AppendLine(" "); msg.AppendLine("result=" + result); if (model == null) { msg.Append("AccessTokenModel=" + model); } else { msg.AppendFormat("errcode={0},errmsg={1}", model.errcode, model.errmsg); } //LogHelper.Save(msg.ToString(), "GetOAuth", LogType.Error, LogTime.day); } return(model); }
/// <summary> /// 刷新access_token /// </summary> /// <param name="refresh_token"></param> /// <returns></returns> public static PageOAuth_AccessToken RefreshAccessToken(string refresh_token) { if (string.IsNullOrWhiteSpace(refresh_token)) { throw new ArgumentException("参数值无效", nameof(refresh_token)); } string url = string.Format("https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={0}&grant_type=refresh_token&refresh_token={1}", Privacy.AppId, refresh_token); string result = HttpRequestHelper.Request(url); PageOAuth_AccessToken accessToken = JsonHelper.DeSerialize <PageOAuth_AccessToken>(result); return(accessToken); }
/// <summary> /// 通过code换取网页授权access_token。 /// 如果网页授权的作用域为snsapi_base,则本步骤中获取到网页授权access_token的同时,也获取到了openid,snsapi_base式的网页授权流程即到此为止。 /// </summary> /// <param name="code"></param> /// <returns></returns> public static PageOAuth_AccessToken GetAccessToken(string code) { if (string.IsNullOrWhiteSpace(code)) { throw new ArgumentException("参数值无效", nameof(code)); } string url = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", Privacy.AppId, Privacy.AppSecret, code); string result = HttpRequestHelper.Request(url); PageOAuth_AccessToken accessToken = JsonHelper.DeSerialize <PageOAuth_AccessToken>(result); return(accessToken); }
/// <summary> /// 通过code换取网页授权access_token。 /// 请注意,这里通过code换取的网页授权access_token,与基础支持中的access_token不同。 /// </summary> /// <param name="code"></param> /// <returns></returns> public static AccessTokenModel GetOAuth(string code) { if (string.IsNullOrEmpty(code)) { throw new Exception("code 为必填参数!"); } string url = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", Privacy.AppId, Privacy.AppSecret, code); try { string result = HttpRequestHelper.Request(url); PageOAuth_AccessToken model = JsonHelper.DeSerialize <PageOAuth_AccessToken>(result); if (model == null || string.IsNullOrEmpty(model.openid)) { StringBuilder msg = new StringBuilder("获取授权Openid失败,url=" + url); msg.AppendLine(" "); msg.AppendLine("result=" + result); if (model == null) { msg.Append("AccessTokenModel=" + model); } else { msg.AppendFormat("errcode={0},errmsg={1}", model.errcode, model.errmsg); } //LogHelper.Save(msg.ToString(), "GetOAuth", LogType.Error, LogTime.day); } return(model); } catch (Exception Ex) { LogHelper.Save(Ex); throw Ex; } }