当前位置:网站首页>微信小程序获取手机号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");
边栏推荐
- Selenium:简介
- UE4 rays flashed from mouse position detection
- MySQL实践总结-
- pytorch、tensorflow对比学习—功能组件(激活函数、模型层、损失函数)
- (2022 Nioke Duo School IV) D-Jobs (Easy Version) (3D prefix or)
- The sword refers to Offer 68 - I. Nearest Common Ancestor of Binary Search Trees
- UE4 制作遇到的问题
- The difference between scheduleWithFixedDelay and scheduleAtFixedRate
- typescript21-接口和类型别名的对比
- safari浏览器怎么导入书签
猜你喜欢

About making a progress bar for software initialization for Qt

(Codeforce 757) E. Bash Plays with Functions

(2022牛客多校四)K-NIO‘s Sword(思维)

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

(2022牛客多校四)D-Jobs (Easy Version)(三维前缀或)

(2022 Nioke Duo School IV) D-Jobs (Easy Version) (3D prefix or)

程序员代码面试指南 CD15 生成窗口最大值数组

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

7月编程排行榜来啦!这次有何新变化?

typescript22-接口继承
随机推荐
初识shell脚本
state compressed dp
状态压缩dp
typescript28 - value of enumeration type and data enumeration
Selenium:浏览器操作
【目标检测】YOLOv7理论简介+实践测试
Li Chi's work and life summary in July 2022
WPF入门项目必知必会-初步了解数据绑定 binding
Selenium:元素等待
Lawyer Interpretation | Guns or Roses?Talking about Metaverse Interoperability from the Battle of Big Manufacturers
(more than 2022 cattle school four) A - Task Computing + dynamic programming (sort)
剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
pytorch、tensorflow对比学习—功能组件(激活函数、模型层、损失函数)
Power button (LeetCode) 212. The word search II (2022.07.31)
Selenium:元素判断
Immutable
Error: AttributeError: module 'matplotlib' has no attribute 'figure'
律师解读 | 枪炮还是玫瑰?从大厂之争谈元宇宙互操作性
PaddleX部署推理模型和GUI界面测试结果不一致的解决方法
Selenium:操作JS