一、獲取OpenIDjson
#region 獲取微信支付的OpenId /// <summary> /// 獲取微信支付的OpenId /// </summary> /// <returns></returns> public string GetPayOpenID() { try {
string code = Request.QueryString["code"]; if (string.IsNullOrEmpty(code)) { string url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + Constant.WX_PAY_APPID + "&redirect_uri=" + HttpUtility.UrlEncode(Request.Url.ToString()) + "&response_type=code&scope=snsapi_base&state=123#wechat_redirect"; } else { string url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + Constant.WX_PAY_APPID + "&secret=" + Constant.WX_PAY_APPSECRET + "&code=" + code + "&grant_type=authorization_code";
string jsonResult = new System.Net.Http.HttpClient().GetStringAsync(url).Result; JToken jtoken = JToken.Parse(jsonResult); string openid = jtoken.Value<string>("openid"); return openid; } } catch (Exception ex) { } return ""; } #endregion
二、獲取Tokenapi
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=***&secret=***
響應結果:微信
{ "access_token":"O3PwqnKoiiYmsHeAh8viWLQyhGRrGU6RT9o53pvlmhIBWQnTXeZDSkYNw6YufzIDUspzQguvxtmLXtAWmQd2NmurXKa4N4PsbwG7RvI25pqzSC3-cLl50iqSW5VaZ4xmGXQgAFAJAT", "expires_in":7200 }
三、使用Token和OpenID獲取用戶信息app
https://api.weixin.qq.com/cgi-bin/user/info?access_token=***&openid=***&lang=zh_CN
響應結果:微信支付
{ "subscribe":1, "openid":"oeQDZt0n4VCZ70wy***", "nickname":"背上***旅行", "sex":1, "language":"zh_CN", "city":"昌平", "province":"北京", "country":"中國", "headimgurl":"http://wx.qlogo.cn/mmopen/kBwGJuwqK9**********************ibVUEpgFE90LH3b3uj7AYRjZP/0", "subscribe_time":1474964999, "unionid":"oGCG8t5**********jPQTPw", "remark":"", "groupid":0, "tagid_list":[ ] }