First step: a small program the code is simple: call wx. The login (), to obtain a temporary login credentials code values (every request is random values),
Step 2: call wx. Request key: case on the link url value is not others, is this your c # to write the background program packaging API path (such as local preview is localhost), through the background c # program to access to the official website (https://api.weixin.qq.com/sns/jscode2session? Appid=APPID& Secret=SECRET& Js_code=JSCODE& Grant_type=authorization_code),
Step 3: put the results of the c # program to access the website had returned to the small program
Specific code:
small program end:
wx. Login ({
Success (res) {
If (res) code) {
The console. The log (" user login product certificate "+ res. Code).
Wx. Request ({
Url: 'http://localhost:59132/api/user',
Data: {
Code: res. Code,
},
Success (res) {
The console. The log (" for openid "+ res. Data);
}
})
}
}
})
Code parsing: wx. Login website has a detailed explanation, fixed format fixed syntax, direct copy, note: I'm over here is the url path is Visual Studio local preview url, that is the path of c # source code, can not understand MVC apis to create c # and access to
c # program end:
[HttpGet]
Public string login (string code)
{
String js_code=code. The ToString ();
String serviceAddress="HTTP://https://api.weixin.qq.com/sns/jscode2session? Appid=APPID& Secret=SECRET& Js_code js_code="+ +" & amp; Grant_type=authorization_code ";
HttpWebRequest request=(HttpWebRequest) WebRequest. Create (serviceAddress);
Request. The Method="GET";
Request. The ContentType="text/HTML. Charset=utf-8 ";
HttpWebResponse response=(HttpWebResponse) request. The method GetResponse ();
Stream myResponseStream=response. GetResponseStream ();
StreamReader myStreamReader=new StreamReader (myResponseStream, Encoding UTF8);
String retString=myStreamReader. ReadToEnd ();
MyStreamReader. Close ();
MyResponseStream. Close ();
Return retString;
}
Parsing code: the code is also fixed syntax format copy directly, it can be seen that this code is to return a call retString value, this value contains the information such as openid,
This code into a value, that is, to the small program code values, pass through the url, https://api.weixin.qq.com/sns/jscode2session? said before Appid=APPID& Secret=SECRET& Js_code=JSCODE& Grant_type=authorization_code read site is written in this application, the url contains three variables, one is the code value, one is the appId, one is the appSecret, code values to get a small program, appId and appSecret in the setting of our small application platform is directly replace the appId and SECRET values in the link, the two write die, the execution result
Beginners it may be difficult to understand, don't understand, can give me a message, make progress together!
CodePudding user response:
Need to how to write to the database to opentid write to