Introduction to how to obtain AccessToken when developing WeChat using .net

高洛峰
Release: 2017-03-16 14:43:29
Original
1638 people have browsed it

The example in this article shares the method of obtaining AccessToken for your reference. The specific content is as follows

AccessToken obtaining 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<Access_token>(retString);
    res.access_token = token.access_token;
    res.expires_in = token.expires_in;
  }
  return res;
}
Copy after login

Access_token class structure

public class Access_token
{
  public Access_token()
  {
    // 
    //TODO:用于验证Access_token是否过期实体
    // 
  }
  string _access_token;
  string _expires_in;
 
  /// <summary> 
  /// 获取到的凭证  
  /// </summary> 
  public string access_token
  {
    get { return _access_token; }
    set { _access_token = value; }
  }
 
  /// <summary> 
  /// 凭证有效时间,单位:秒 
  /// </summary> 
  public string expires_in
  {
    get { return _expires_in; }
    set { _expires_in = value; }
  }
}
Copy after login

JsonHelper.ParseFromJson Method

/// <summary> 
/// 将JSON对象转换为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);
  }
}
Copy after login


The above is the detailed content of Introduction to how to obtain AccessToken when developing WeChat using .net. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!