当前位置:网站首页>微信小程序用户登录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失败");
}
边栏推荐
- Risk strategy important steps of tuning method
- Selenium:简介
- (2022 Niu Ke Duo School IV) K-NIO's Sword (Thinking)
- Excel做题记录——整数规划优化模型
- Excuse me, only primary key columns can be queried using sql in table storage. Does ots sql not support non-primary keys?
- y83. Chapter 4 Prometheus Factory Monitoring System and Actual Combat -- Advanced Prometheus Alarm Mechanism (14)
- MySQL Practice Summary -
- (2022 Nioke Duo School IV) D-Jobs (Easy Version) (3D prefix or)
- 2022年超全的Android面经(附含面试题|进阶资料)
- Selenium:浏览器操作
猜你喜欢

Pyspark Machine Learning: Vectors and Common Operations

罗技鼠标体验记录
![[target detection] YOLOv7 theoretical introduction + practical test](/img/ff/a83acbf9dd5cc2f907f3538d287842.png)
[target detection] YOLOv7 theoretical introduction + practical test

USB3.0:VL817Q7-C0的LAYOUT指南(三)

typescript23-元组

这里有110+公开的专业数据集

High Numbers | 【Re-integration】Line Area Score 880 Examples

mysql中解决存储过程表名通过变量传递的方法

产品经理访谈 | 第五代验证码的创新与背景

state compressed dp
随机推荐
Selenium:操作JS
4D line-by-line analysis and implementation of Transformer, and German translation into English (3)
pytroch、tensorflow对比学习—专栏介绍
LeetCode 1189. “气球” 的最大数量
PAT serie b write the number 1002
可持久化线段树
PaddleX部署推理模型和GUI界面测试结果不一致的解决方法
Selenium:表单切换
pytroch、tensorflow对比学习—搭建模型范式(低阶、中阶、高阶API示例)
UE4 rays flashed from mouse position detection
用控件当画笔获得bitmap代码记录
Selenium:元素判断
typescript26-字面量类型
高数 | 【重积分】线面积分880例题
typescript20-接口
pytorch、tensorflow对比学习—功能组件(优化器、评估指标、Module管理)
(Codeforce 757) E. Bash Plays with Functions
风险策略调优中重要的三步分析法
UE4 模型OnClick事件不生效的两种原因
罗技鼠标体验记录