当前位置:网站首页>微信小程序接口调用凭证(获取token)auth.getAccessToken接口开发
微信小程序接口调用凭证(获取token)auth.getAccessToken接口开发
2022-08-01 05:09:00 【市井榴芒】
1、接口规范
小程序接口调用凭证auth.getAccessToken接口规范参考链接
1.1、接口请求地址
GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
1.2、接口请求参数
appid
:小程序 appIdsecret
:小程序 appSecretgrant_type
:授权类型,填写 client_credential
1.3、接口响应参数(返回的 JSON 数据包)
access_token
:获取到的凭证expires_in
:凭证有效时间,单位:秒。目前是7200秒之内的值。errcode
:错误码***(异常情况才会有错误码返回,正常是没有该字段返回的)***errmsg
:错误信息
2、准备工作
需要准备请求接口的2个参数:appid
:小程序 appId,需要小程序注册申请流程获得;secret
:小程序 appSecret,需要在小程序开发配置选项里面经由管理员扫码授权生成;
3、代码
//getWxAccessToken()方法用来将所有请求参数拼接起来形成最终的请求URL,可以在postman/apifox里面先行测通
//APP_ID=小程序 appId
//APP_SECRET=小程序 appSecret
String urlAccessToken = getWxAccessToken(APP_ID, APP_SECRET);
//发起请求
String respAccessToken = HttpUtil.get(urlAccessToken);
if(StringUtil.isNull(respAccessToken)){
throw new ServiceException("803","请求access_token失败");
}
//将响应转为Json对象
JSONObject respAccessTokenObj = JSONObject.parseObject(respAccessToken);
//获取accessToken
String accessToken = respAccessTokenObj.getString("access_token");
if(accessToken==null){
throw new ServiceException("804","获取access_token失败");
}
边栏推荐
猜你喜欢
Dry goods!How to Construct SRv6-TE Performance Test Environment Using Instrumentation
About making a progress bar for software initialization for Qt
Robot_Framework:常用内置关键字
High Numbers | 【Re-integration】Line Area Score 880 Examples
typescript20-接口
typescript23-元组
(2022牛客多校四)H-Wall Builder II(思维)
Selenium: Manipulating Cookies
typescript19-对象可选参数
(2022 Nioke Duo School IV) D-Jobs (Easy Version) (3D prefix or)
随机推荐
(more than 2022 cattle school four) A - Task Computing + dynamic programming (sort)
Dry goods!How to Construct SRv6-TE Performance Test Environment Using Instrumentation
typescript19-对象可选参数
干货!如何使用仪表构造SRv6-TE性能测试环境
typescript24 - type inference
pytorch、tensorflow对比学习—功能组件(激活函数、模型层、损失函数)
API设计笔记:pimpl技巧
Power button (LeetCode) 212. The word search II (2022.07.31)
typescript28-枚举类型的值以及数据枚举
Robot_Framework:断言
关于给Qt做一个软件初始化的进度条
Robot_Framework: Assertion
Check控件
typescript28 - value of enumeration type and data enumeration
Robot_Framework:关键字
Selenium:弹窗处理
Selenium:下拉框操作
华为Android开发面试后得出的面试秘诀
typescript27-枚举类型呢
Selenium:操作Cookie