阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

微信公众号-接口凭据

106次阅读
没有评论

共计 1695 个字符,预计需要花费 5 分钟才能阅读完成。

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 请求

微信公众号 - 接口凭据

正确的返回值

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

微信公众号 - 接口凭据

错误的返回值

{"errcode":40013, "errmsg":"invalid appid" }

DJango 服务代码实现

myApp/accessToken.py

import time import requests import json class AccessToken: _access_token = {"token":"", "updatatime":time.time()} @classmethod def updateAccessToken(cls, grant_type, appid, secret): url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=%s&appid=%s&secret=%s"%(grant_type, appid, secret) res = requests.get(url) resDit = json.loads(res.content) if resDit["access_token"]: cls._access_token["token"] = resDit["access_token"] cls._access_token["updatatime"] = time.time() @classmethod def getAccessToken(cls, grant_type, appid, secret): if not cls._access_token["token"] or (time.time() - cls._access_token["updatatime"]) >= 6500: # 更新 token cls.updateAccessToken(grant_type, appid, secret) return cls._access_token["token"]

myApp/urls.py

from django.urls import path, re_path from myApp import views urlpatterns = [path(r'index/', views.index), path(r'weixin/', views.weixin), path(r'access/', views.access), ]

myApp/views.py

from django.shortcuts import render, HttpResponse from django.views.decorators.csrf import csrf_exempt import hashlib from myApp.accessToken import AccessToken def index(request): return HttpResponse("sunck is a good man") @csrf_exempt def weixin(request): pass def access(request): access_token = AccessToken.getAccessToken("client_credential", "wxffde55b11cc79754", "84e86527f090d6238ea1c0b96f5fc753") return HttpResponse(access_token)

自有平台获取参数:

微信公众号 - 接口凭据

测试平台获取参数:

微信公众号 - 接口凭据

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

微信公众号 - 接口凭据

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-05-26发表,共计1695字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中