国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
ASP.NET Web API實(shí)現(xiàn)微信公眾平臺(tái)開發(fā)(二)access
承接上一篇,今天主要講述如何實(shí)現(xiàn)定時(shí)獲取微信access_token功能的實(shí)現(xiàn)。
access_token
首先我們根據(jù)微信的開發(fā)指南,任何對(duì)微信的操作都要使用合法獲取的access_token,微信獲取access_token限制每日次數(shù),且每次token有效時(shí)間為7200秒。
獲取token的API:
//http請(qǐng)求方式: GEThttps://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
參數(shù)說明:
參數(shù)是否必須說明
grant_type
獲取access_token填寫client_credential
appid
第三方用戶唯一憑證
secret
第三方用戶唯一憑證密鑰,即appsecret
返回?cái)?shù)據(jù)示例:
{"access_token":"ACCESS_TOKEN","expires_in":7200}
詳細(xì)請(qǐng)看官方文檔:http://mp.weixin.qq.com/wiki/11/0e4b294685f817b95cbed85ba5e82b8f.html
定時(shí)任務(wù)
根據(jù)其限制,這里獲取token的策略為:一小時(shí)一次,采用定時(shí)任務(wù)的方式執(zhí)行。定時(shí)任務(wù)的實(shí)現(xiàn)方式采用Azure WebJob來實(shí)現(xiàn),具體原理為:Azure定時(shí)調(diào)用任務(wù)程序訪問指定Url,調(diào)用相應(yīng)方法更新靜態(tài)access_token字段。
定時(shí)任務(wù)程序?yàn)楹?jiǎn)單控制臺(tái)程序,點(diǎn)擊Web項(xiàng)目右鍵添加->新建Web Job項(xiàng)目,將會(huì)生成默認(rèn)模版項(xiàng)目,簡(jiǎn)單修改一下,其代碼如下:
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 = 20 }); } }
Web程序?qū)?yīng)方法為:
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; }
控制臺(tái)程序發(fā)布為zip文件,上傳至Azure作業(yè)儀表板,并且設(shè)定為計(jì)劃任務(wù)。
查看日志
可以看到我們預(yù)定返回的access_token字段,說明我們已經(jīng)在服務(wù)器上更新了Access_Token信息,這樣可以確保下一步的動(dòng)作。
關(guān)于Azure Web Job的更多信息請(qǐng)看:http://www.windowsazure.cn/documentation/articles/web-sites-create-web-jobs/?fb=002
另外定時(shí)任務(wù)有很多種方法,推薦好基友@Ed_Wang的一篇博客,提供了另一種方式:http://edi.wang/post/2014/7/18/how-to-run-schedule-jobs-in-aspnet
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
java 微信授權(quán)登錄配置
Http API觸發(fā)小程序云函數(shù)案例
企業(yè)API接口設(shè)計(jì)(token、timestamp、sign)之具體實(shí)現(xiàn)
[C#] 自己開發(fā)實(shí)現(xiàn)OAuth做webapi認(rèn)證 | 教程大全
C# 并發(fā)隊(duì)列ConcurrentQueue
web實(shí)現(xiàn)QQ第三方登錄
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服