当前位置:网站首页>微信小程序获取手机号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");
边栏推荐
猜你喜欢
![[target detection] YOLOv7 theoretical introduction + practical test](/img/ff/a83acbf9dd5cc2f907f3538d287842.png)
[target detection] YOLOv7 theoretical introduction + practical test

PAT serie b write the number 1002

typescript21 - Comparison of Interfaces and Type Aliases

Selenium: Manipulating Cookies

typescript23-元组

pytroch、tensorflow对比学习—搭建模型范式(构建模型方法、训练模型范式)

Robot_Framework:断言

pytorch、tensorflow对比学习—计算图和微分机制

备战金九银十,如何顺利通过互联网大厂Android的笔面试?

可持久化线段树
随机推荐
About making a progress bar for software initialization for Qt
safari浏览器怎么导入书签
MySQL-DML language-database operation language-insert-update-delete-truncate
请问shake数据库中为什么读取100个collection 后,直接就退出了,不继续读了呢?
剑指 Offer 68 - II. 二叉树的最近公共祖先
Pyspark机器学习:向量及其常用操作
pytorch、tensorflow对比学习—功能组件(优化器、评估指标、Module管理)
MySQL-Data Definition Language-DDLdatebase define language
The difference between scheduleWithFixedDelay and scheduleAtFixedRate
干货!如何使用仪表构造SRv6-TE性能测试环境
Excuse me, only primary key columns can be queried using sql in table storage. Does ots sql not support non-primary keys?
4D line-by-line analysis and implementation of Transformer, and German translation into English (3)
2022年超全的Android面经(附含面试题|进阶资料)
7月编程排行榜来啦!这次有何新变化?
关于给Qt做一个软件初始化的进度条
Robot_Framework:断言
Selenium: Introduction
(2022 Niu Ke Duo School IV) N-Particle Arts (Thinking)
Selenium:元素定位
李迟2022年7月工作生活总结