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&appid=%s&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>]) >= <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/