.NET platform, application and development of micro-nails: Get userid

Work needs to develop micro-nail applications and applets, before there is contact with the development of Alipay small number of programs and life, the process is no great difference, I realize at record here nailed to develop micro applications with ASP.NET MVC, and achieve access to the user's userid. Brother my limited technology, some of the methods herein named or badly written, please also give pointers.

There Developer Platform SDK nails on each platform, I also have to download the corresponding version of the .NET SDK, but not full they get to know me first with the most earth approach: initiate direct http request to get the data I need, If you have time later I will also make up .NET.

1, registration nail micro applications:

After registration is complete, continue to improve under application configuration, such as IP white list.

In the Basics> Development Information (Legacy), you can see there is a Corpid, official documents "to obtain micro applications Bintang authorization code", is the need for the ID.

 

 

 2, get Code:

Bintang first need to get an authorization code based on corpid. According to the official wording of the document, this step is very simple, I write directly in the event of a button, the returned result in respect with the code I need.

1 // Get Bintang authorization code 
 2 $ ( "# getAuthorCode" ) .click (function () { 
 3             dd.ready (function () { 
 4 // dd.ready parameters for the callback function, trigger-ready environment in preparation, call jsapi need to ensure that after the callback function triggers calls, otherwise invalid. 
 5                 dd.runtime.permission.requestAuthCode ({ 
 6 corpId: "here to fill corpid" ,  7  onSuccess: function (the Result) {  8 Alert ( "Succeed" ) ;. 9 Alert (result.code); 10 },. 11 onFail: function (ERR) 12 is {Alert ( "Fail" ); 13 is } 14 }); 15 }); var ARR = 16 []; arr.push. 17 ( { "AuthorCode": authorCode }); 18 ajax("/Home/GetAuthorCode", arr, "text", false); 19 });

 

 3, get access_token:

Look at how the official document is written.

Ah, Yes, in fact I mainly see here "request address" in the background as long as the parameters assigned to it, appropriate back-end code is as follows.

After receiving the data returned by the nails, deserialized data, obtained access_token.

 1  /// <summary>
 2         /// 获取授权访问令牌
 3         /// </summary>
 4         /// <returns></returns>
 5         public JsonResult GetAccessToken()
 6         {
 7             string AppKey = "后台提供的key";
 8             string AppSecret = "后台提供的secret";
 9             string url = "https://oapi.dingtalk.com/gettoken?appkey=" + AppKey + "&appsecret=" + AppSecret;
10             WebRequest request = WebRequest.Create(url); 11 WebResponse response = request.GetResponse(); 12 StreamReader reader = new StreamReader(response.GetResponseStream(),Encoding.ASCII); 13 string responseData = reader.ReadToEnd(); 14        var data = JsonConvert.DeserializeObject<CommonModel>(responseData); //json字符串反序列化
         string token = data.access_token;
22             return Json(new { Result = true, token }, JsonRequestBehavior.AllowGet);
23         }

4, get userid:

According to the official documentation for userid you need to use the code and token.

When I get to a few minutes in front of the background code and token, direct and initiate http request as an argument, this time the reported error code 40078,

 

 

 That is to get in front of the code need to re-enter authentication, where the authentication mechanism can carefully check out the official documentation, I was not very clear, time will be useful to look at the document.
Then I get back and token code like:
///. 1 <Summary> 
 2 /// Get the userid 
 . 3 /// </ Summary> 
 . 4 /// <Returns> </ Returns> 
 . 5 public a JsonResult getUserId () 
 . 6         { 
 . 7 // retrieve code 
 . 8 String = authorCode the Request.Form [0 ]; 
 . 9 the JavaScriptSerializer new new JS = the JavaScriptSerializer ();  10 CommonModel selectListss = new new CommonModel ();  . 11 = Data var js.Deserialize <List <CommonModel >> (authorCode); Code String 12 is = Data [0 ] .AuthorCode; 13 14 // Get token 15 string AppKey = "key provided in the background" ; 16 appsecret String = "background provided Secret"; 17 string url = "https://oapi.dingtalk.com/gettoken?appkey=" + AppKey + "&appsecret=" + AppSecret; 18 WebRequest request = WebRequest.Create(url); 19 WebResponse response = request.GetResponse(); 20 StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII); 21 string responseData = reader.ReadToEnd();

          var data = JsonConvert.DeserializeObject <CommonModel> ( responseData); // json string deserialization
          string token = data.access_token;

30             
31             //获取userid
32             string getUserId_Url = "https://oapi.dingtalk.com/user/getuserinfo?access_token=" + token + "&code=" + Code;
33             WebRequest getUserId_request = WebRequest.Create(getUserId_Url);
34             WebResponse getUserId_response = getUserId_request.GetResponse();
35             StreamReader getUserId_reader = new StreamReader(getUserId_response.GetResponseStream(), Encoding.ASCII);
36             string getUserId_responseData = getUserId_reader.ReadToEnd();
37

          var data = JsonConvert.DeserializeObject <CommonModel> (getUserId_responseData); // json string deserialization

          string userid = data.userid;

45             return Json(new { Result = true,userid }, JsonRequestBehavior.AllowGet);
46         }

Above, the project will be written and published to pack my IIS server, PC-side (cell phone lines) nail open the application to see the effect:

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/seaquakear/p/11431000.html