Access Token
Access Token plays an important role in the development of WeChat public platform interface, which is equivalent to the key to access various interfaces. Only by getting this key can we call other special interfaces.
access_token is the globally unique ticket for the public number, which calls each interface using access_token.Normal access_The token expires for 7200 seconds, and repeated fetches result in the last access_fetchedToken is invalid.
Public numbers can use AppID and AppSecret to call this interface to get access_token.AppID and AppSecret are available in development mode (need to be a developer with no account exception).Note that all WeChat interfaces must be invoked using the https protocol.
http request method: GET
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
The C#.NET code is as follows:
/// <summary>
/// Obtain AccessToken
/// </summary>
/// <returns></returns>
public static string getAccessToken()
{
string url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + AppID + "&secret=" + AppSecret;
HttpWebRequest webrequest = (HttpWebRequest)System.Net.HttpWebRequest.Create(url);
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();//Request connection and return data
Stream stream = webresponse.GetResponseStream();//Convert returned data to stream file
byte[] rsByte = new Byte[webresponse.ContentLength]; //Convert Stream File to Byte Array
try
{
stream.Read(rsByte, 0, (int)webresponse.ContentLength);
string responseStr = System.Text.Encoding.Default.GetString(rsByte, 0, rsByte.Length).ToString().Replace("{", "").Replace("}", "");
string[] jsons = responseStr.Split(',');
if (jsons.Length == 2)
{
string[] param = jsons[0].Split(':');
if (param.Length == 2 && param[0] == "\"access_token\"")
{
string tempAccessToken = param[1].Replace("\"", "");
return tempAccessToken ;
}
else
{
return "error";
}
}
return "error";
}
catch
{
return "error";
}
}
Normally, WeChat returns the following JSON packets to the public number:
{"access_token":"ACCESS_TOKEN","expires_in":7200}
jsapi_ticket
Jsapi_A ticket is a temporary ticket that the public number uses to invoke the WeChat JS interface.Normally, jsapi_ticket has a validity period of 7200 seconds through access_token to get.Since getting jsapi_ticket has a very limited number of api calls and frequently refreshes jsapi_Tickets can limit api calls and affect their business, so developers must globally cache jsapi_in their servicesTicket.
http request method: GET
https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi
The C#.NET code is as follows:
private static string getJsapiTicket()
{
string interfaceUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + AccessToken + "&type=jsapi";
HttpWebRequest webrequest = (HttpWebRequest)System.Net.HttpWebRequest.Create(interfaceUrl);
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();//Request connection and return data
Stream stream = webresponse.GetResponseStream();//Convert returned data to stream file
byte[] rsByte = new Byte[webresponse.ContentLength]; //Convert Stream File to Byte Array
try
{
stream.Read(rsByte, 0, (int)webresponse.ContentLength);
string strb = System.Text.Encoding.Default.GetString(rsByte, 0, rsByte.Length).ToString().Replace("{", "").Replace("}", "");
if ((strb.ToString().IndexOf("\"errcode\":42001") != -1) || (strb.ToString().IndexOf("\"errcode\":40001") != -1) || (strb.ToString().IndexOf("\"errcode\":40014") != -1) || (strb.ToString().IndexOf("\"errcode\":41001") != -1))
{
//access_token error
}
else if (strb.ToString().IndexOf("\"errcode\":0,\"errmsg\":\"ok\"") != -1)
{
string[] jsons = strb.Split(',');
if (jsons.Length == 4)
{
string[] param = jsons[2].Split(':');
if (param.Length == 2 && param[0] == "\"ticket\"")
{
string tempJsapiTicket = param[1].Replace("\"", "");
return tempJsapiTicket ;
}
else
{
return "error";
}
}
return "error";
}
else
{
return "error";
}
}
catch
{
return "error";
}
}
The following JSON was successfully returned:
{
"errcode":0,
"errmsg":"ok",
"ticket":"bxLdikRXVbTPdHSM05e5u5sUoXNKd8-41ZO3MhKoyN5OfkWITDGgnr2fwJ0m9E8NYzWKVZvdVtaUgWvsdshFKA",
"expires_in":7200
}