当前位置:网站首页>微信小程序用户登录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失败");
}
边栏推荐
猜你喜欢

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

Robot_Framework: Assertion

typescript21 - Comparison of Interfaces and Type Aliases

The sword refers to Offer 68 - I. Nearest Common Ancestor of Binary Search Trees

typescript26-字面量类型

Pyspark Machine Learning: Vectors and Common Operations

pytroch、tensorflow对比学习—功能组件(数据管道、回调函数、特征列处理)
![[target detection] YOLOv7 theoretical introduction + practical test](/img/ff/a83acbf9dd5cc2f907f3538d287842.png)
[target detection] YOLOv7 theoretical introduction + practical test

冲刺金九银十,Android开发面试(内含面试资料|面试题|源码)

风险策略调优中重要的三步分析法
随机推荐
Robot_Framework: commonly used built-in keywords
移动应用恶意攻击激增500% 三六零天御为APP免费构建安全屏障
MySQL-Data Definition Language-DDLdatebase define language
Immutable
typescript21 - Comparison of Interfaces and Type Aliases
(Codeforce 757)E. Bash Plays with Functions(积性函数)
UE4 制作遇到的问题
Selenium: mouse, keyboard events
typescript25-类型断言
UE4 从鼠标位置射出射线检测
关于给Qt做一个软件初始化的进度条
图片更新之后Glide加载依旧是原来的图片问题
What should I do if the neural network cannot be trained?
pytroch、tensorflow对比学习—专栏介绍
Excel做题记录——整数规划优化模型
WPF入门项目必知必会-初步了解数据绑定 binding
4D line-by-line analysis and implementation of Transformer, and German translation into English (3)
请问shake数据库中想把源的db0的数据同步到目的db5,参数怎么设置呢?
Robot_Framework:常用内置关键字
typescript22-接口继承