AccessToken and accesstoken

Source: Internet
Author: User

AccessToken and accesstoken

How to obtain AccessToken

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<Access_token>(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. //} string _ access_token; string _ expires_in; /// <summary> /// obtained credential /// </summary> public string access_token {get {return _ access_token;} set {_ access_token = value ;}} /// <summary> // validity period of the credential, in the unit of seconds /// </summary> public string expires_in {get {return _ expires_in ;} set {_ expires_in = value ;}}}

JsonHelper. ParseFromJson Method

/// <Summary> /// convert the JSON object to Model /// </summary> /// <typeparam name = "T"> </typeparam> /// <param name = "szJson"> </param> // <returns> </returns> public static T ParseFromJson <T> (string szJson) {T obj = Activator. createInstance <T> (); using (MemoryStream MS = new MemoryStream (Encoding. UTF8.GetBytes (szJson) {DataContractJsonSerializer serializer = new DataContractJsonSerializer (obj. getType (); return (T) serializer. readObject (MS );}}

 

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.