/// <summary> /// 根据Code获得OpenID /// </summary> /// <param name="code">Code</param> /// <returns>OpenID</returns> public string GetOpenIDByCode(string code) { WeChatDataModel data = new WeChatDataModel(); data.SetValue("appid", _config.APPID); data.SetValue("secret", _config.APPSECRET); data.SetValue("grant_type", "authorization_code"); data.SetValue("code", code); string url = "https://api.weixin.qq.com/sns/jscode2session?" + data.ToUrlParams(); string result = WeChatHttpManager.Get(url); JsonData jd = JsonMapper.ToObject(result); string OpenID = (string)jd["openid"]; return(OpenID); }
/// <summary> /// 获得OpenID和AccessToken /// </summary> /// <param name="code">网址Code</param> public void GetOpenidAndAccessTokenFromCode(string code) { try { //构造获取openid及access_token的url WeChatDataModel data = new WeChatDataModel(); data.SetValue("appid", _config.APPID); data.SetValue("secret", _config.APPSECRET); data.SetValue("code", code); data.SetValue("grant_type", "authorization_code"); string url = "https://api.weixin.qq.com/sns/oauth2/access_token?" + data.ToUrlParams(); //请求url以获取数据 string result = WeChatHttpManager.Get(url); //保存access_token,用于收货地址获取 JsonData jd = JsonMapper.ToObject(result); _accessToken = (string)jd["access_token"]; //获取用户openid OpenID = (string)jd["openid"]; } catch (Exception ex) { throw new MWeChatException(ex.ToString()); } }