my.getAuthCode

Call the API to obtain the authorization code. The authorization code can be used to obtain the access token, so as to obtain the app user's information, such as user ID and nickname.
For more information, refer to
User authorization.

Sample code

copy
my.getAuthCode({
  scopes: ['auth_base'],
  success: (res) => {
    my.alert({
      content: res.authCode,
    });
  },
  fail: (res) => {
      console.log(res.authErrorScopes)
  },
});

Parameters

Property

Type

Required

Description

scopes

Array

Yes

The scope of authorization. For more information about its valid values, refer to the following Scopes section.

success

Function

No

Callback function upon call success.

fail

Function

No

Callback function upon call failure.

complete

Function

No

Callback function upon call completion (to be executed upon either call success or failure).

Scopes

Valid value

Description

auth_base

Authorized to silently obtain user ID only.

auth_user

Authorized to obtain user ID and other user information.

SEND_MESSAGE

Authorized to send notifications to the user via SMS, PUSH, or INBOX.

USER_NICKNAME

Authorized to obtain the user nickname.

USER_NAME

Authorized to obtain the user name.

USER_LOGIN_ID

Authorized to obtain the user's login ID.

PLAINTEXT_USER_LOGIN_ID

Authorized to obtain the user's plaintext login ID.

HASH_LOGIN_ID

Authorized to obtain the hash user login ID.

USER_AVATAR

Authorized to obtain the user avatar.

USER_GENDER

Authorized to obtain the user's gender.

USER_BIRTHDAY

Authorized to obtain the user's birthday.

USER_NATIONALITY

Authorized to obtain the user's nationality.

USER_CONTACTINFO

Authorized to obtain the user's contact information.

USER_ADDRESS

Authorized to obtain the user's address.

PLAINTEXT_MOBILE_PHONE

Authorized to obtain the user's plaintext phone number.

Note: auth_base is used to silently obtain user ID only. Silent authorization does not pop the frame and directly obtains user information. All the other scopes are used for proactive user authorization.

Callback function

The incoming parameter is of the Object type with the following attributes:

Property

Type

Required

Description

authCode

String

Yes

Authorization code.

authErrorScopes

Key-value

Yes

The scope that fails to grant authorization. key is the scope and value is the error.

authSuccessScopes

Array

Yes

The scope that succeeds to grant authorization.

Successful response example

copy
{
    "authCode":"1591*****0204",
    "authSuccessScopes":['auth_base']
}

Failure response example

copy
{
    "authErrorScopes":{
       "auth_user":"1***1"
    }
}

Error code

Error code

Error message

Further action

2

Invalid parameters

Check whether the scopes request parameter is specified correctly. For details, see Scopes.

3

Unknown error

  1. The error may be caused because your authorization service is abnormal. Try to check your authorization for the mini program. For more, see the OAuthService SPI.
  2. The error is caused by unknown reasons. Please check whether your mini program is working properly and whether the network is working properly.

10

Empty data

The error is caused because the request to authorize the network failed or timeout. Retry to call this API. If the problem still exists, please contact overseas_support@service.alibaba.com for assistance.

11

User's authorization is cancelled

Explain to the user about the purpose and necessity of obtaining the user information. Then help to instruct the user to action again.