当前位置:网站首页>微信小程序获取手机号phonenumber.getPhoneNumber接口开发
微信小程序获取手机号phonenumber.getPhoneNumber接口开发
2022-08-01 05:09:00 【市井榴芒】
1、接口规范
小程序获取手机号phonenumber.getPhoneNumber接口规范参考链接
1.1、接口请求地址
POST https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=ACCESS_TOKEN
注意:是POST
方式
1.2、接口请求参数
access_token
:接口调用凭证,需要使用表单方式
请求;code
:手机号获取凭证,跟用户登录的code不一样!,需要使用JSON方式
请求;
1.3、接口响应参数(返回的 JSON 数据包)
phone_info
:用户手机号信息,返回的是个Object
对象,里边包含了手机号信息errcode
:错误码(异常情况才会有错误码返回,正常是没有该字段返回的)errmsg
:错误信息
2、准备工作
需要准备请求接口的2个参数:access_token
:接口调用凭证,其实就是token,获取token可以参考之前的博文 点我跳转code
:获取手机号 code,需要前端配置小程序信息获得,前端支持一下即可;
3、代码
//getWxPhone()方法用来将所有请求参数拼接起来形成最终的请求URL,可以在postman/apifox里面先行测通
//将code参数封装到JSON对象中进行请求
//将access_token拼接到接口URL中进行请求
String urlPhone = getWxPhone(accessToken);
Map<String, Object> params = new HashMap<>();
params.put("code",code2);
//发起请求
String respPhone = HttpUtil.post(urlPhone, params);
if(StringUtil.isNull(respPhone)){
throw new ServiceException("805","请求phone失败");
}
//将响应转为JSON对象
JSONObject respPhoneObj = JSONObject.parseObject(respPhone);
JSONObject phoneObj = (JSONObject) respPhoneObj.get("phone_info");
if(phoneObj==null){
throw new ServiceException("906","获取phone失败");
}
//获取手机号
String phone = phoneObj.getString("phoneNumber");
边栏推荐
- 零序电流继电器器JL-8C-12-2-2
- LeetCode 1189. “气球” 的最大数量
- 请问shake数据库中想把源的db0的数据同步到目的db5,参数怎么设置呢?
- Selenium:上传、下载文件
- typescript19-对象可选参数
- (Codeforce 757)E. Bash Plays with Functions(积性函数)
- 力扣(LeetCode)212. 单词搜索 II(2022.07.31)
- Typescript22 - interface inheritance
- Logitech Mouse Experience Record
- Error: AttributeError: module 'matplotlib' has no attribute 'figure'
猜你喜欢
Lawyer Interpretation | Guns or Roses?Talking about Metaverse Interoperability from the Battle of Big Manufacturers
状态压缩dp
typescript21-接口和类型别名的对比
What should I do if the neural network cannot be trained?
Robot_Framework:断言
MySQL-DML语言-数据库操作语言-insert-update-delete-truncate
(2022 Nioke Duo School IV) H-Wall Builder II (Thinking)
Optional parameters typescript19 - object
Excel做题记录——整数规划优化模型
typescript24-类型推论
随机推荐
万字逐行解析与实现Transformer,并进行德译英实战(一)
产品经理访谈 | 第五代验证码的创新与背景
UE4 制作遇到的问题
typescript19-对象可选参数
MySQL Practice Summary -
力扣(LeetCode)212. 单词搜索 II(2022.07.31)
typescript24-类型推论
Selenium:简介
可持久化线段树
ApiFile
万字逐行解析与实现Transformer,并进行德译英实战(三)
UE4 从鼠标位置射出射线检测
Robot_Framework: Assertion
Selenium: mouse, keyboard events
MySQL-Data Operation-Group Query-Join Query-Subquery-Pagination Query-Joint Query
基于Arduino制作非接触式测温仪
Selenium:元素等待
Typescript20 - interface
请问shake数据库中想把源的db0的数据同步到目的db5,参数怎么设置呢?
typescript28-枚举类型的值以及数据枚举