Access Token

在微信公众平台接口开发中,Access Token占据了一个很重要的地位,相当于进入各种接口的钥匙,拿到这个钥匙才有调用其他各种特殊接口的权限。 
access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token。正常情况下access_token有效期为7200秒,重复获取将导致上次获取的access_token失效。 
公众号可以使用AppID和AppSecret调用本接口来获取access_token。AppID和AppSecret可在开发模式中获得(需要已经成为开发者,且帐号没有异常状态)。注意调用所有微信接口时均需使用https协议。

http请求方式: GET

https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

C#.NET代码如下:

/// <summary>
/// 获取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();//请求连接,并反回数据
Stream stream = webresponse.GetResponseStream();//把返回数据转换成流文件 byte[] rsByte = new Byte[webresponse.ContentLength]; //把流文件转换为字节数组 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";
}
}

  

正常情况下,微信会返回下述JSON数据包给公众号:

{"access_token":"ACCESS_TOKEN","expires_in":7200}

  

jsapi_ticket

jsapi_ticket是公众号用于调用微信JS接口的临时票据。正常情况下,jsapi_ticket的有效期为7200秒,通过access_token来获取。由于获取jsapi_ticket的api调用次数非常有限,频繁刷新jsapi_ticket会导致api调用受限,影响自身业务,开发者必须在自己的服务全局缓存jsapi_ticket 。

http请求方式: GET

https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi

C#.NET代码如下:

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();//请求连接,并反回数据
Stream stream = webresponse.GetResponseStream();//把返回数据转换成流文件
byte[] rsByte = new Byte[webresponse.ContentLength]; //把流文件转换为字节数组 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错误
}
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";
}
}

  成功返回如下JSON:

{
"errcode":0,
"errmsg":"ok",
"ticket":"bxLdikRXVbTPdHSM05e5u5sUoXNKd8-41ZO3MhKoyN5OfkWITDGgnr2fwJ0m9E8NYzWKVZvdVtaUgWvsdshFKA",
"expires_in":7200
}

  

05-11 22:19