WeChat JS-SDK sharing Function of. Net implementation code, js-sdk.net

Source: Internet
Author: User

JS-SDK sharing Function of. Net implementation code, js-sdk.net

What is a JS-SDK interface?

To facilitate developers to implement internal web pages (web pages accessed Based on browsers), such as photo taking, Image Selection, voice, location, and other mobile phone systems, and convenient developers directly use sharing, sweep and other unique capabilities, launched the JS-SDK of the overall development kit for developers to conveniently use.

Sharing

Official documentation provides php, java, and node. js and python sample code, but no c # version, in order to make up for the majority. net user requirements, I copied the example code logic of the php version. and added the sharing feature to the front-end of the webpage.

Program Implementation

Flowchart

The key class in the program is JSSDK, which contains all the logic processes of server-side request authentication. The following is the process flowchart:

Key code analysis

To ensure the security of data transmission between a third-party server and the server, all interfaces are called using https. net references a relatively high version (.. Net 4.5 +.

private string httpGet(string url){  if (url.StartsWith("https"))    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;  HttpClient httpClient = new HttpClient();  httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));  HttpResponseMessage response = httpClient.GetAsync(url).Result;  if (response.IsSuccessStatusCode)  {    string result = response.Content.ReadAsStringAsync().Result;    return result;  }  return null;}

Obtain the access_token from the local access_token.aspx. If it does not exist or expires (7000 seconds), it will be retrieved from the server again.

private string getAccessToken(){  string accessToken = string.Empty;  var data = JObject.Parse(getAspxFile("access_token.aspx", ASPX_HEAD[1]));  if (data != null && long.Parse(data["expire_time"].ToString()) < Utils.ConvertTimeStamp(DateTime.Now))  {    string url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="      + this.appId + "&secret=" + this.appSecret;    var jRes = JObject.Parse(httpGet(url));    accessToken = jRes["access_token"].ToString();    if (!string.IsNullOrEmpty(accessToken))    {      data["expire_time"] = Utils.ConvertTimeStamp(new DateTime()) + 7000;      data["access_token"] = accessToken;      setAspxFile("access_token.aspx", data.ToString(), ASPX_HEAD[1]);    }  }  else    accessToken = data["access_token"].ToString();  return accessToken;}

Obtain jsapi_ticket in the same principle as access_token.

private string getJsApiTicket(){  string ticket = string.Empty;  var data = JObject.Parse(getAspxFile("jsapi_ticket.aspx", ASPX_HEAD[0]));  if (data != null && long.Parse(data["expire_time"].ToString()) < Utils.ConvertTimeStamp(DateTime.Now))  {    string accessToken = getAccessToken();    string url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token="      + accessToken;    var jRes = JObject.Parse(httpGet(url));    ticket = jRes["ticket"].ToString();    if (!string.IsNullOrEmpty(ticket))    {      data["expire_time"] = Utils.ConvertTimeStamp(new DateTime()) + 7000;      data["jsapi_ticket"] = ticket;      setAspxFile("jsapi_ticket.aspx", data.ToString(), ASPX_HEAD[0]);    }  }  else    ticket = data["jsapi_ticket"].ToString();  return ticket;}

Complete code

Https://github.com/stozen/jssdk-wxshare

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.