zoukankan      html  css  js  c++  java
  • ASP.NET微信公众号获取AccessToken

    access_token是公众号的全局唯一接口调用凭据,公众号调用各接口时都需使用access_token。开发者需要进行妥善保存。access_token的存储至少要保留512个字符空间。access_token的有效期目前为2个小时,需定时刷新,重复获取将导致上次获取的access_token失效。下面是使用ASP.NET WebForm实现获取微信公众号AccessToken的例子。附代码和文档。

    先看效果图,没有输入appid时的效果:



    获取正确的结果:


    其中长长一串的字符串就是AccessToken,7200秒表示有效时间为2小时。
    获取的结果是json格式,可以使用Newtonsoft.Json.dll对json信息进行处理,在下一篇中给出示例代码。参考:http://hovertree.com/h/bjag/l0aqhe0f.htm

    HtAccessToken.aspx页面代码:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="HtAccessToken.aspx.cs" Inherits="ExampleHoverTree.HtWechat.HtAccessToken" %>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title>获取AccessToken示例_何问起</title>
    </head>
    <body>
        <h3>何问起代码练习</h3>
        <form id="form1" runat="server">
            <div>
                appid:<asp:TextBox runat="server" ID="textBox_appid" />
                <br />secret:<asp:TextBox runat="server" ID="textBox_secret" />
                <br /><asp:Button runat="server" ID="button_get"  Text="获取Access_token" OnClick="button_get_Click"/>
                <br />
                <asp:Literal runat="server" ID="literal_tips" />
            </div>
        </form>
    </body>
    </html>

    HtAccessToken.aspx.cs代码:

    using HoverTree.HoverTreeFrame.HtWeb;
    using System;
    using System.Text;
    
    namespace ExampleHoverTree.HtWechat
    {
        public partial class HtAccessToken : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void button_get_Click(object sender, EventArgs e)
            {
                literal_tips.Text = HtGetAccessToken(textBox_appid.Text, textBox_secret.Text);
            }
    
            string HtGetAccessToken(string appid, string appsecret)
            {
                string h_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
                    + appid + "&secret=" + appsecret;
    
                return HtWebHelper.GetHtmlByUrl(h_url, Encoding.Default);
            }
        }
    }

    其中,HtWebHelper类的GetHtmlByUrl(string url, System.Text.Encoding enCode)方法请下载HoverTreeTop源码,在HoverTreeFrame项目中。下载地址:http://hovertree.com/h/bjaf/hv6cqe5n.htm

    公众平台的API调用所需的access_token的使用及生成方式说明:
    1、建议公众号开发者使用中控服务器统一获取和刷新Access_token,其他业务逻辑服务器所使用的access_token均来自于该中控服务器,不应该各自去刷新,否则容易造成冲突,导致access_token覆盖而影响业务;
    2、目前Access_token的有效期通过返回的expire_in来传达,目前是7200秒之内的值。中控服务器需要根据这个有效时间提前去刷新新access_token。在刷新过程中,中控服务器对外输出的依然是老access_token,此时公众平台后台会保证在刷新短时间内,新老access_token都可用,这保证了第三方业务的平滑过渡;
    3、Access_token的有效时间可能会在未来有调整,所以中控服务器不仅需要内部定时主动刷新,还需要提供被动刷新access_token的接口,这样便于业务服务器在API调用获知access_token已超时的情况下,可以触发access_token的刷新流程。更多:http://hovertree.com/h/bjag/x6usc7ya.htm

    推荐:http://www.cnblogs.com/roucheng/category/827769.html

  • 相关阅读:
    游戏对战练习
    扩展属性 配置文件
    数据操作类:增删改查(三大类)
    作业
    泛型集合
    Linux下查看文件和文件夹大小
    reids客户端 redis-cli用法
    生产环境下JAVA进程高CPU占用故障排查
    MySQL主从复制与读写分离
    最全面 Nginx 入门教程 + 常用配置解析
  • 原文地址:https://www.cnblogs.com/roucheng/p/wxaccesstoken.html
Copyright © 2011-2022 走看看