手机号 /getPhoneNumber # phonenumber.getPhoneNumber > 本接口应在服务器端调用,详细说明参见[服务端API](https://developers.weixin.qq.com/miniprogram/dev/framework/server-ability/backend-api.html)。 > 本接口支持[云调用](https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/openapi/openapi.html)。需开发者工具版本 >= `1.02.1904090`(最新[稳定版下载](https://developers.weixin.qq.com/miniprogram/dev/devtools/stable.html)),`wx-server-sdk` >= `0.4.0` code换取用户手机号。 每个 code 只能使用一次,code的有效期为5min 调用方式: - [HTTPS 调用](https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/phonenumber/phonenumber.getPhoneNumber.html#method-http) - [云调用](https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/phonenumber/phonenumber.getPhoneNumber.html#method-cloud) ## HTTPS 调用 ### 请求地址 ```text POST https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=ACCESS_TOKEN ``` ### 请求参数 | 属性 | 类型 | 默认值 | 必填 | 说明 | | :------------------------------------ | :----- | :----- | :--- | :----------------------------------------------------------- | | access_token / cloudbase_access_token | string | | 是 | [接口调用凭证](https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html) | | code | string | | 是 | [手机号获取凭证](https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/getPhoneNumber.html) | ### 返回值 ### Object 返回的 JSON 数据包 | 属性 | 类型 | 说明 | | :--------- | :----- | :------------- | | errcode | number | 错误码 | | errmsg | string | 错误提示信息 | | phone_info | Object | 用户手机号信息 | **errcode 的合法值** | 值 | 说明 | 最低版本 | | :---- | :------------------------------------------- | :------- | | 0 | 请求成功 | | | -1 | 系统繁忙,此时请开发者稍候再试 | | | 40029 | 不合法的code(code不存在、已过期或者使用过) | | **phone_info 的结构** | 属性 | 类型 | 说明 | | :-------------- | :----- | :------------------------------------- | | phoneNumber | string | 用户绑定的手机号(国外手机号会有区号) | | purePhoneNumber | string | 没有区号的手机号 | | countryCode | string | 区号 | | watermark | Object | 数据水印 | **watermark 的结构** | 属性 | 类型 | 说明 | | :-------- | :----- | :------------------------- | | appid | string | 小程序appid | | timestamp | number | 用户获取手机号操作的时间戳 | ### 调用示例 ```text curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"code": "e31968a7f94cc5ee25fafc2aef2773f0bb8c3937b22520eb8ee345274d00c144"}' https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=ACCESS_TOKEN& ``` ### 返回结果示例 ```json { "errcode":0, "errmsg":"ok", "phone_info": { "phoneNumber":"xxxxxx", "purePhoneNumber": "xxxxxx", "countryCode": 86, "watermark": { "timestamp": 1637744274, "appid": "xxxx" } } } ``` ## 云调用 > [云调用](https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/openapi/openapi.html)是微信云开发提供的在云函数中调用微信开放接口的能力,需要在云函数中通过 `wx-server-sdk` 使用。 ### 接口方法 ```js openapi.phonenumber.getPhoneNumber ``` > 需在 `config.json` 中配置 `phonenumber.getPhoneNumber` API 的权限,[详情](https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/openapi/openapi.html#usage-3) ### 请求参数 | 属性 | 类型 | 默认值 | 必填 | 说明 | | :--- | :----- | :----- | :--- | :----------------------------------------------------------- | | code | string | | 是 | [手机号获取凭证](https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/getPhoneNumber.html) | ### 返回值 ### Object 返回的 JSON 数据包 | 属性 | 类型 | 说明 | | :-------- | :----- | :------------- | | errCode | number | 错误码 | | errMsg | string | 错误提示信息 | | phoneInfo | Object | 用户手机号信息 | **errCode 的合法值** | 值 | 说明 | 最低版本 | | :--- | :--- | :------- | | 0 | 成功 | | **phoneInfo 的结构** | 属性 | 类型 | 说明 | | :-------------- | :----- | :------------------------------------- | | phoneNumber | string | 用户绑定的手机号(国外手机号会有区号) | | purePhoneNumber | string | 没有区号的手机号 | | countryCode | string | 区号 | | watermark | Object | 数据水印 | **watermark 的结构** | 属性 | 类型 | 说明 | | :-------- | :----- | :------------------------- | | appid | string | 小程序appid | | timestamp | number | 用户获取手机号操作的时间戳 | ### 异常 ### Object 抛出的异常 | 属性 | 类型 | 说明 | | :------ | :----- | :----------- | | errCode | number | 错误码 | | errMsg | string | 错误提示信息 | **errCode 的合法值** | 值 | 说明 | 最低版本 | | :---- | :------------------------------------------- | :------- | | -1 | 系统繁忙,此时请开发者稍候再试 | | | 40029 | 不合法的code(code不存在、已过期或者使用过) | |