当前位置:网站首页>微信小程序获取手机号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");
边栏推荐
- (2022牛客多校四)N-Particle Arts(思维)
- typescript27 - what about enumeration types
- 干货!如何使用仪表构造SRv6-TE性能测试环境
- (2022牛客多校四)K-NIO‘s Sword(思维)
- 4D line-by-line analysis and implementation of Transformer, and German translation into English (3)
- DL-31/6电流继电器
- 华为Android开发面试后得出的面试秘诀
- WPF入门项目必知必会-初步了解数据绑定 binding
- 万字逐行解析与实现Transformer,并进行德译英实战(二)
- Robot_Framework:常用内置关键字
猜你喜欢

(2022 Niu Ke Duo School IV) N-Particle Arts (Thinking)

2022年超全的Android面经(附含面试题|进阶资料)

What should I do if the neural network cannot be trained?

Progressive Reconstruction of Visual Structure for Image Inpainting 论文笔记

Excel做题记录——整数规划优化模型

56:第五章:开发admin管理服务:9:开发【文件上传到,MongoDB的GridFS中,接口】;(把文件上传到GridFS的SOP)
![[MySQL] 多表查询](/img/f0/c158750a5a84155ee82729daba2bb3.png)
[MySQL] 多表查询

MySQL-Data Definition Language-DDLdatebase define language

Dry goods!How to Construct SRv6-TE Performance Test Environment Using Instrumentation

DL-31/6电流继电器
随机推荐
mysql中解决存储过程表名通过变量传递的方法
(more than 2022 cattle school four) A - Task Computing + dynamic programming (sort)
「以云为核,无感极速」顶象第五代验证码
state compressed dp
(2022牛客多校四)A-Task Computing (排序+动态规划)
7月编程排行榜来啦!这次有何新变化?
PaddleX部署推理模型和GUI界面测试结果不一致的解决方法
Selenium:简介
万字逐行解析与实现Transformer,并进行德译英实战(一)
Pyspark机器学习:向量及其常用操作
pytorch、tensorflow对比学习—计算图和微分机制
LeetCode 387. 字符串中的第一个唯一字符
4D line-by-line analysis and implementation of Transformer, and German translation into English (3)
Pyspark Machine Learning: Vectors and Common Operations
Selenium:操作Cookie
typescript24-类型推论
2022年湖南工学院ACM集训第六次周测题解
文件的异步读写
PAT class B 1001 (3n+1) conjecture
LeetCode 9. 回文数