How to get AccessToken using. net for WeChat development

Source: Internet
Author: User
This article describes how to use. net development to obtain an AccessToken. This article provides an example of how to obtain an AccessToken for your reference. The details are as follows:

AccessToken acquisition method

public static Access_token GetAccessToken(){  string formatString = String.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", AppId, AppSecret);   Access_token res = new Access_token();  HttpWebRequest request = (HttpWebRequest)WebRequest.Create(formatString);  request.Method = "GET";  request.ContentType = "text/html;charset=UTF-8";  HttpWebResponse response = (HttpWebResponse)request.GetResponse();  Stream myResponseStream = response.GetResponseStream();  StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));  string retString = myStreamReader.ReadToEnd();  myStreamReader.Close();  myResponseStream.Close();  if (retString.IndexOf("7200") > 0)  {    Access_token token = new Access_token();    token = JsonHelper.ParseFromJson(retString);    res.access_token = token.access_token;    res.expires_in = token.expires_in;  }  return res;}

Access_token class structure

Public class Access_token {public Access_token () {// TODO: used to verify whether the Access_token has expired the object //} string _ access_token; string _ expires_in ;////// The obtained credential ///Public string access_token {get {return _ access_token;} set {_ access_token = value ;}}////// Valid time of the credential, in seconds ///Public string expires_in {get {return _ expires_in;} set {_ expires_in = value ;}}}

JsonHelper. ParseFromJson method

////// Convert a JSON object to a Model //////
 //////
 Public static T ParseFromJson
 
  
(String szJson) {T obj = Activator. CreateInstance
  
   
(); Using (MemoryStream MS = new MemoryStream (Encoding. UTF8.GetBytes (szJson) {DataContractJsonSerializer serializer = new DataContractJsonSerializer (obj. getType (); return (T) serializer. readObject (MS );}}
  
 

The preceding section describes how to obtain the AccessToken for. net development. For more information, see other related articles on php!

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.