/// <summary> /// 如果网页授权作用域为snsapi_userinfo,则此时开发者可以通过access_token和openid拉取用户信息了。 /// </summary> /// <param name="oauth_accesstoken"></param> /// <returns></returns> public PubRecPersonInfo getUserInfo(OAuth_AccessToken oauth_accesstoken) { PubRecPersonInfo temp = null; try { if ("snsapi_userinfo".Equals(oauth_accesstoken.scope.ToLower())) { string result = string.Empty; string url = string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN", oauth_accesstoken.access_token, oauth_accesstoken.openid); result = HTTPHelper.GetRequest(url); if (!result.Contains("errcode")) { temp = new PubRecPersonInfo(result); } } if (temp == null) { log.Info("getUserInfo result: PubReqPersonInfo is null"); } } catch (Exception e) { log.Error("getUserInfo error", e); } return(temp); }
/// <summary> /// 检验授权凭证(access_token)是否有效 /// </summary> /// <param name="oauth_accesstoken"></param> /// <returns></returns> public bool checkOAuth_access_token(OAuth_AccessToken oauth_accesstoken) { bool sign = false; string result = string.Empty; try { string url = string.Format("https://api.weixin.qq.com/sns/auth?access_token={0}&openid={1}", oauth_accesstoken.access_token, oauth_accesstoken.openid); result = HTTPHelper.GetRequest(url); JObject jo = (JObject)JsonConvert.DeserializeObject(result); if ("ok".Equals(jo["errmsg"].ToString())) { sign = true; } else { log.Info(string.Format("checkOAuth_access_token failed: {0} ", result)); } } catch (Exception e) { log.Error("checkOAuth_access_token error", e); } return(sign); }
/// <summary> /// 通过code换取网页授权access_token /// </summary> /// <param name="code"></param> /// <param name="agentid"></param> /// <returns></returns> public OAuth_AccessToken getOAuth_access_token(string code) { OAuth_AccessToken robject = null; if (!string.IsNullOrEmpty(code)) { string result = string.Empty; try { string url = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", config.AppID, config.Secret, code); result = HTTPHelper.GetRequest(url); if (!result.Contains("errcode")) { robject = new OAuth_AccessToken(result); } else { log.Info("Pub getOAuth_access_token Failed! "); } } catch (Exception e) { log.Error("Pub getOAuth_access_token error", e); } } return(robject); }
/// <summary> /// 由于access_token拥有较短的有效期,当access_token超时后,可以使用refresh_token进行刷新,refresh_token拥有较长的有效期(7天、30天、60天、90天),当refresh_token失效的后,需要用户重新授权。 /// </summary> /// <param name="refresh_token"></param> /// <returns></returns> public OAuth_AccessToken refreshOAuth_access_token(string refresh_token) { OAuth_AccessToken robject = null; string result = string.Empty; try { string url = string.Format("https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={0}&grant_type=refresh_token&refresh_token={1}", config.AppID, refresh_token); result = HTTPHelper.GetRequest(url); robject = new OAuth_AccessToken(result); } catch (Exception e) { log.Error("refreshOAuth_access_token error", e); } return(robject); }