当前位置:网站首页>微信小程序用户登录auth.code2Session接口开发
微信小程序用户登录auth.code2Session接口开发
2022-08-01 05:09:00 【市井榴芒】
1、接口规范
小程序用户登录auth.code2Session接口规范参考链接
1.1、接口请求地址
GET https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
1.2、接口请求参数
appid:小程序 appIdsecret:小程序 appSecretjs_code:登录时获取的 codegrant_type:授权类型,此处只需填写 authorization_code
1.3、接口响应参数(返回的 JSON 数据包)
openid:用户唯一标识session_key:会话密钥unionid:用户在开放平台的唯一标识符,若当前小程序已绑定到微信开放平台帐号下会返回errcode:错误码***(异常情况才会有错误码返回,正常是没有该字段返回的)***errmsg:错误信息
2、准备工作
需要准备请求接口的3个参数:appid:小程序 appId,需要小程序注册申请流程获得;secret:小程序 appSecret,需要在小程序开发配置选项里面经由管理员扫码授权生成;js_code:登录时获取的 code,需要前端配置小程序信息获得,前端支持一下即可;
3、代码
//getWxOpenid()方法用来将所有请求参数拼接起来形成最终的请求URL,可以在postman/apifox里面先行测通
//code=前端提供的登录code
//APP_ID=小程序 appId
//APP_SECRET=小程序 appSecret
String urlOpenid = getWxOpenid(code, APP_ID, APP_SECRET);
//发起请求
String respOpenid = HttpUtil.get(urlOpenid);
if(StringUtil.isNull(respOpenid)){
throw new ServiceException("801","请求openid失败");
}
//将响应转为Json对象
JSONObject respOpenidObj = JSONObject.parseObject(respOpenid);
//获取openid
String openid = respOpenidObj.getString("openid");
if(openid==null){
throw new ServiceException("802","获取openid失败");
}
边栏推荐
猜你喜欢

scheduleWithFixedDelay和scheduleAtFixedRate的区别

UE4 模型OnClick事件不生效的两种原因

typescript26-字面量类型

(2022牛客多校四)N-Particle Arts(思维)

Excel record of integer programming optimization model to solve the problem

万字逐行解析与实现Transformer,并进行德译英实战(一)

state compressed dp

PAT serie b write the number 1002

移动应用恶意攻击激增500% 三六零天御为APP免费构建安全屏障

typescript28 - value of enumeration type and data enumeration
随机推荐
typescript23-tuple
typescript24 - type inference
文件的异步读写
ModuleNotFoundError: No module named ‘tensorflow.keras‘报错信息的解决方法
律师解读 | 枪炮还是玫瑰?从大厂之争谈元宇宙互操作性
UE4 制作遇到的问题
safari浏览器怎么导入书签
请问shake数据库中为什么读取100个collection 后,直接就退出了,不继续读了呢?
备战金九银十,如何顺利通过互联网大厂Android的笔面试?
PAT serie b write the number 1002
(Codeforce 757) E. Bash Plays with Functions
pytroch、tensorflow对比学习—功能组件(数据管道、回调函数、特征列处理)
(2022 Nioke Duo School IV) D-Jobs (Easy Version) (3D prefix or)
API设计笔记:pimpl技巧
4D line-by-line analysis and implementation of Transformer, and German translation into English (3)
万字逐行解析与实现Transformer,并进行德译英实战(二)
Selenium: mouse, keyboard events
The sword refers to Offer 68 - I. Nearest Common Ancestor of Binary Search Trees
typescript28-枚举类型的值以及数据枚举
Selenium: Manipulating Cookies