my.getAuthCode

Call the API to obtain the authorization code. The authorization code is used to obtain the access token to get basic user information.

System requirement

Follow the system requirements below based on your super app:

  • For Alipay China (Alipay CN): Alipay CN 10.2.38 or above. If the version is earlier, we recommend you try to implement compatibility.
  • For other Alipay+ partner apps, contact the technical support of your super app.

Sample code

copy
my.getAuthCode({
  scopes: ['auth_user'],
  success: (res) => {
    my.alert({
      content: res.authCode,
    });
  },
});

Parameters

Property

Data Type

Required

Description

scopes

Array[String]

Yes

The scopes of authorization.

The following scopes of authorization are supported both in Alipay China and other Alipay+ partner apps:

  • auth_base
  • auth_user

The following scopes of authorization are only supported in other Alipay+ partner apps:

  • SEND_MESSAGE

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 description

Scopes

Description

auth_base

Authorized to silently obtain the user ID only.

auth_user

Authorized to obtain user information.

SEND_MESSAGE

Authorized to send notifications to users via SMS/PUSH/INBOX.

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

Property

Data Type

Required

Description

error

Number

No

The error code

errorMessage

String

No

The error message

authCode

String

Yes

The authorization code

authErrorScopes

Object

Yes

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

authSuccessScopes

Array[String]

Yes

The scope that succeeds to grant authorization.

Successful response example

copy
{
    "authCode":"28100613DFLQDKXJQRSMKIXZCI"
  "authErrorScopes":{}
  "authSuccessScopes":['auth_user']
}

Failure response example

Two types of failure responses are listed below:

  1. Authorization failed, abnormal, or canceled (with error and errorMessage returned but no auth-related parameters):
copy
// Canceled by user
{
    "error": 11,
    "errorMessage": "Unauthorized to get user info"
}
// The obtained authCode value is empty
{
    "error": 10,
    "errorMessage": "Empty authCode"
}
  1. Authorization failed for certain authorization scopes (with auth-related parameters returned). The error message is specified in the value of the authErrorScopes object:
copy
// Authorization failed when the value of scope is auth_user
{
    "authCode": "",
    "authSuccessScopes": [],
    "authErrorScopes": {
        "auth_user": "10001"
    }
}