前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >微信公众号-接口凭据

微信公众号-接口凭据

作者头像
星哥玩云
发布2022-09-14 17:58:50
1K0
发布2022-09-14 17:58:50
举报
文章被收录于专栏:开源部署

access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token。开发者需要进行妥善保存。access_token的存储至少要保留512个字符空间。access_token的有效期目前为2个小时,需定时刷新,重复获取将导致上次获取的access_token失效

原理:

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

正确的返回值

代码语言:javascript
复制
{
    "access_token":"ACCESS_TOKEN",
    "expires_in":7200
}

错误的返回值

代码语言:javascript
复制
{
    "errcode":40013,
    "errmsg":"invalid appid"
}

DJango服务代码实现

myApp/accessToken.py

代码语言:javascript
复制
<span class="hljs-keyword">import</span> time
<span class="hljs-keyword">import</span> requests
<span class="hljs-keyword">import</span> json

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AccessToken</span>:</span>
    _access_token = {
        <span class="hljs-string">"token"</span>:<span class="hljs-string">""</span>,
        <span class="hljs-string">"updatatime"</span>:time.time()
    }
<span class="hljs-meta">    @classmethod</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">updateAccessToken</span><span class="hljs-params">(cls, grant_type, appid, secret)</span>:</span>
        url = <span class="hljs-string">"https://api.weixin.qq.com/cgi-bin/token?grant_type=%s&amp;appid=%s&amp;secret=%s"</span>%(grant_type, appid, secret)
        res = requests.get(url)
        resDit = json.loads(res.content)
        <span class="hljs-keyword">if</span> resDit[<span class="hljs-string">"access_token"</span>]:
            cls._access_token[<span class="hljs-string">"token"</span>] = resDit[<span class="hljs-string">"access_token"</span>]
            cls._access_token[<span class="hljs-string">"updatatime"</span>] = time.time()

<span class="hljs-meta">    @classmethod</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">getAccessToken</span><span class="hljs-params">(cls, grant_type, appid, secret)</span>:</span>
        <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> cls._access_token[<span class="hljs-string">"token"</span>] <span class="hljs-keyword">or</span> (time.time() - cls._access_token[<span class="hljs-string">"updatatime"</span>]) &gt;= <span class="hljs-number">6500</span>:
            <span class="hljs-comment"># 更新token</span>
            cls.updateAccessToken(grant_type, appid, secret)
        <span class="hljs-keyword">return</span> cls._access_token[<span class="hljs-string">"token"</span>]

myApp/urls.py

代码语言:javascript
复制
<span class="hljs-keyword">from</span> django.urls <span class="hljs-keyword">import</span> path, re_path
<span class="hljs-keyword">from</span> myApp <span class="hljs-keyword">import</span> views

urlpatterns = [
    path(<span class="hljs-string">r'index/'</span>, views.index),
    path(<span class="hljs-string">r'weixin/'</span>, views.weixin),
    path(<span class="hljs-string">r'access/'</span>, views.access),
]

myApp/views.py

代码语言:javascript
复制
<span class="hljs-keyword">from</span> django.shortcuts <span class="hljs-keyword">import</span> render, HttpResponse
<span class="hljs-keyword">from</span> django.views.decorators.csrf <span class="hljs-keyword">import</span> csrf_exempt
<span class="hljs-keyword">import</span> hashlib
<span class="hljs-keyword">from</span> myApp.accessToken <span class="hljs-keyword">import</span> AccessToken

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">index</span><span class="hljs-params">(request)</span>:</span>
    <span class="hljs-keyword">return</span> HttpResponse(<span class="hljs-string">"sunck is a good man"</span>)

<span class="hljs-meta">@csrf_exempt</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">weixin</span><span class="hljs-params">(request)</span>:</span>
    <span class="hljs-keyword">pass</span>
            
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">access</span><span class="hljs-params">(request)</span>:</span>
    access_token = AccessToken.getAccessToken(<span class="hljs-string">"client_credential"</span>, <span class="hljs-string">"wxffde55b11cc79754"</span>, <span class="hljs-string">"84e86527f090d6238ea1c0b96f5fc753"</span>)
    <span class="hljs-keyword">return</span> HttpResponse(access_token)      

自有平台获取参数:

测试平台获取参数:

浏览器地址栏输入:http://39.107.226.105/access/

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档