跳转到内容

微信快捷登录

刀刀
0字
0分钟
2026/8/16

获取令牌

button 组件 open-type 的值设置为 getPhoneNumber,当用户点击并同意之后,可以通过事件回调如 bindgetphonenumber 获取到动态令牌code,然后把code传到开发者后台,并在开发者后台调用微信后台提供的 phonenumber.getPhoneNumber 接口,通过code来换取用户手机号。每个code有效期为 5 分钟,且只能消费一次。

温馨提示

getPhoneNumber 返回的 codewx.login 返回的 code 作用是不一样的,不能混用。

代码示例

html
<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber"></button>
Page({ getPhoneNumber (e) { console.log(e.detail.code) // 调用登录接口 } })

返回参数说明

参数类型说明最低版本
codeString动态令牌。可通过动态令牌换取用户手机号。使用方法详情 phonenumber.getPhoneNumber 接口

bug

出现 getPhoneNumber:fail:access denied

因为该账号未认证,官方文档明确表明微信小程序要完成认证才可使用。

拓展:获取手机号

功能描述

该接口用于将 code 换取用户手机号。 说明,每个 code 只能使用一次,code 的有效期为 5min。

调用方式

HTTPS 调用

txt
POST https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=ACCESS_TOKEN

第三方调用

  • 调用方式以及出入参和 HTTPS 相同,仅是调用的 token 不同
  • 该接口所属的权限集 id 为:18
  • 服务商获得其中之一权限集授权后,可通过使用authorizer_access_token代商家进行调用

请求参数

属性类型必填说明
access_tokenstring接口调用凭证,该参数为 URL 参数,非 Body 参数。使用access_token或者authorizer_access_token
codestring手机号获取凭证

返回参数

属性类型说明
errcodenumber错误码
errmsgstring错误信息
phone_infoobject用户手机号信息
phoneNumberstring用户绑定的手机号(国外手机号会有区号)
purePhoneNumberstring没有区号的手机号
countryCodestring区号
watermarkobject数据水印
timestampnumber用户获取手机号操作的时间戳
appidstring小程序 appid

错误码具体含义如下:

错误码错误码取值解决方案
-1system error系统繁忙,此时请开发者稍候再试
40029code 无效js_code 无效
json
{
  "code": "e31968a7f94cc5ee25fafc2aef2773f0bb8c3937b22520eb8ee345274d00c144"
}
json
{
  "errcode": 0,
  "errmsg": "ok",
  "phone_info": {
    "phoneNumber": "xxxxxx",
    "purePhoneNumber": "xxxxxx",
    "countryCode": 86,
    "watermark": {
      "timestamp": 1637744274,
      "appid": "xxxx"
    }
  }
}

贡献者

The avatar of contributor named as 刀刀 刀刀
The avatar of contributor named as duyidao duyidao
The avatar of contributor named as Orange Orange
The avatar of contributor named as v_duyilin v_duyilin
The avatar of contributor named as 平安喜乐 平安喜乐

页面历史

刀刀博客累计访客 人;文档累计访问量共