微信小程序 获取临时登录凭证

 

原文链接:https://www.cnblogs.com/dujian123/p/11184781.html

微信小程序需要两步:

1. 获取登录凭证code

2.调用 auth.code2Session 接口 换区用户唯一标识openid和会话密钥 session_key

官网网址

https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html

https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html

C# 后台获取session_key和openid

string AppID  ="xxxxxxxxxxxxxxxx";

string AppSecret  ="xxxxxxxxxxxxxxxx";

    string serviceAddress = $"https://api.weixin.qq.com/sns/jscode2session?appid=" + AppID  + "&secret="+ AppSecret + "&js_code=" + code + "&grant_type=authorization_code";

 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
request.Method = "GET";
 request.ContentType = "textml;charset=UTF-8";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
string jsonData = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();

string jsonString = jsonData;
JObject json = JObject.Parse(jsonString);
string openid = json["openid"].ToString();

string sessionKey= json["session_key"].ToString();

 

 

你可能感兴趣的:(C#,方法)