ASP.NET Web API实现微信公众平台开发(二)access_token与定时任务

时间:2022-03-08 14:05:26

承接上一篇,今天主要讲述如何实现定时获取微信access_token功能的实现。

access_token

首先我们根据微信的开发指南,任何对微信的操作都要使用合法获取的access_token,微信获取access_token限制每日次数,且每次token有效时间为7200秒。

获取token的API:

//http请求方式: GET
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

参数说明:

参数 是否必须 说明
grant_type 获取access_token填写client_credential
appid 第三方用户唯一凭证
secret 第三方用户唯一凭证密钥,即appsecret

返回数据示例:

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

详细请看官方文档:http://mp.weixin.qq.com/wiki/11/0e4b294685f817b95cbed85ba5e82b8f.html

定时任务

根据其限制,这里获取token的策略为:一小时一次,采用定时任务的方式执行。定时任务的实现方式采用Azure WebJob来实现,具体原理为:Azure定时调用任务程序访问指定Url,调用相应方法更新静态access_token字段。

定时任务程序为简单控制台程序,点击Web项目右键添加->新建Web Job项目,将会生成默认模版项目,简单修改一下,其代码如下:

public class Functions
{
// This function will be triggered based on the schedule you have set for this WebJob
// This function will enqueue a message on an Azure Queue called queue
[NoAutomaticTrigger]
public static void ManualTrigger(TextWriter log, int value, [Queue("queue")] out string message)
{
log.WriteLine("Function is invoked with value={0}", value);
message = GetAccessToken(log).Result;
Console.WriteLine(message);
log.WriteLine("Following message will be written on the Queue={0}", message);
} public static string Url = "http://cwwebservice.azurewebsites.net/api/wx?method=token";
static async Task<string> GetAccessToken(TextWriter log)
{
var client = new HttpClient();
var result = await client.GetStringAsync(Url);
return result;
}
}
class Program
{
// Please set the following connection strings in app.config for this WebJob to run:
// AzureWebJobsDashboard and AzureWebJobsStorage
static void Main()
{
var host = new JobHost();
// The following code will invoke a function called ManualTrigger and
// pass in data (value in this case) to the function
host.Call(typeof(Functions).GetMethod("ManualTrigger"), new { value = }); }
}

ASP.NET Web API实现微信公众平台开发(二)access_token与定时任务

Web程序对应方法为:

 private static string Access_Token = string.Empty;

//刷新access_token字段
public async Task<HttpResponseMessage> Get(string method)
{
var response = new HttpResponseMessage();
if (method == "token")
{
var api = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxc56a0a6310833ff9&secret=05bbade59f505c93378f5be963ba3eeb";
var client = new HttpClient();
var token = await client.GetStringAsync(api);
response.Content = new StringContent(token);
var json = JObject.Parse(token);
if (json["access_token"] != null)
Access_Token = (string)json["access_token"];
}
return response;
}

控制台程序发布为zip文件,上传至Azure作业仪表板,并且设定为计划任务。

ASP.NET Web API实现微信公众平台开发(二)access_token与定时任务

ASP.NET Web API实现微信公众平台开发(二)access_token与定时任务

ASP.NET Web API实现微信公众平台开发(二)access_token与定时任务

查看日志

ASP.NET Web API实现微信公众平台开发(二)access_token与定时任务

ASP.NET Web API实现微信公众平台开发(二)access_token与定时任务

可以看到我们预定返回的access_token字段,说明我们已经在服务器上更新了Access_Token信息,这样可以确保下一步的动作。

关于Azure Web Job的更多信息请看:http://www.windowsazure.cn/documentation/articles/web-sites-create-web-jobs/?fb=002

另外定时任务有很多种方法,推荐好基友@Ed_Wang的一篇博客,提供了另一种方式:http://edi.wang/post/2014/7/18/how-to-run-schedule-jobs-in-aspnet