/v2/authorizations/applyToken

POST /v2/authorizations/applyToken

With this API call, a merchant can obtain an access token from the super app. The merchant is then authorized by users to provide services on the mini program.

Note:

  • Before calling this API, call the my.getAuthCode JSAPI to obtain an authorization code from the super app as the request parameter. Then call this API to exchange for an access token from the super app.
  • When the original access token expires, use the refresh token to exchange for a new access token directly. In this scenario, this API can be used independently.
  • An access token should be kept in the merchant server only, which means it should not be returned to the mini program.

Structure

A message consists of a header and body. The following sections are focused on the body structure. For the header structure, see:

Request parameters

Field

Data type

Required

Description

Example

appId

String

Yes

Indicates the unique ID assigned by Mini Program Platform to identify a mini program.

  • Maximum length: 32 characters
  • Characters not allowed: special characters such as @ # ?

Note: Obtain this field via the my.getAppIdSync JSAPI or Mini Program Platform.

"3333010071465913xxx"

authClientId 

String 

Yes

Indicates the unique ID assigned by the super app to identify an authorized merchant.

  • Maximum length: 128 characters
  • Characters not allowed: special characters such as @ # ?

"202016726873874774774xxxx"

grantType

String 

Yes

Indicates the way the authorized merchant obtains an access token. Valid values are:

  • AUTHORIZATION_CODE: Exchange for an access token.
  • REFRESH_TOKEN: Exchange for a new access token when the original one expires.

"AUTHORIZATION_CODE"

customerBelongsTo

String 

No

Indicates the super app that a user uses. Valid values are:

  • ALIPAY_CN: Alipay CN
  • ALIPAY_HK: Alipay HK
  • ALIPAY_MO: Alipay MO
  • TNG: Touch 'n Go
  • GCASH: Gcash
  • DANA: Dana
  • KAKAOPAY: KakaoPay
  • BKASH: bKash
  • CHOPE: Chope
  • TRUEMONEY: TrueMoney

"TNG"

authCode

String

No

The authorization code is used to exchange for an access token. Mini programs can obtain an authorization code via the my.getAuthCode JSAPI and then send it to the merchant. Then the merchant is authorized to use the authorization code to exchange for an access token.

  • Maximum length: 64 characters
  • Characters not allowed: special characters such as @ # ?
  • Can be Null.

Note: This field is required when the value of grantType is AUTHORIZATION_CODE.

"2810111301lGZcM9CjlF91WH00039190xxxx"

refreshToken

String 

No

The refresh token is used to exchange for a new access token when the original one expires. With the refresh token, a new access token can be obtained without further interaction with the user.

  • Maximum length: 128 characters
  • Characters not allowed: special characters such as @ # ?
  • Can be Null.

Note: This field is required when the value of grantType is REFRESH_TOKEN.

"2810111301lGZcM9CjlF91WH00039190xxxx"

extendInfo

String

No

Indicates the extended information about this API.

  • Maximum length: 4096 characters
  • Characters not allowed: special characters such as @ # ?
  • Can be Null.
copy
{
    "memo": "memo"
}

Response parameters

Field

Data type

Required

Description

Example

result

Result

Yes

Indicates the request result such as status and error codes.

copy
{
  "resultCode": "SUCCESS",
  "resultStatus": "S",
  "resultMessage": "success"
}

accessToken

String

No

The access token is used to access user information. For the specific information that can be accessed, see the my.getAuthCode JSAPI.

  • Maximum length: 128 characters
  • Characters not allowed: special characters such as @ # ?
  • Can be Null.

Note: This field must be returned when the authorization request is successful.

"281010033AB2F588D14B43238637264FCA5AAF35xxxx"

accessTokenExpiryTime

Datetime

No

Indicates when an access token expires. For example, in the payment scenario, once the access token expires, the authorized merchant cannot use this token to debit the user's account.

The value follows the ISO 8601 standard format. For example, "2019-11-27T12:01:01+08:30".

Note: This field must be returned when the authorization request is successful.

"2019-06-06T12:12:12+08:00"

refreshToken

String

No

The refresh token is used to exchange for a new access token when the original one expires. With the refresh token, a new access token can be obtained without further interaction with the user.

  • Maximum length: 128 characters
  • Characters not allowed: special characters such as @ # ?
  • Can be Null.

Note: This field must be returned when the authorization request is successful.

"2810100334F62CBC577F468AAC87CFC6C9107811xxxx"

refreshTokenExpiryTime

Datetime

No

Indicates when the refresh token expires. Once the refresh token expires, the authorized merchant cannot use this token to exchange for a new access token.

The value follows the ISO 8601 standard format. For example, "2019-11-27T12:01:01+08:30".

Note: This field must be returned when the authorization request is successful.

"2019-06-08T12:12:12+08:00"

customerId

String

Yes

Indicates the unique ID assigned by Mini Program Platform to identify a user.

  • Maximum length: 64 characters
  • Characters not allowed: special characters such as @ # ?

"1000001119398804xxxx"

extendInfo

String

No

Indicates the extended information about this API.

  • Maximum length: 4096 characters
  • Characters not allowed: special characters such as @ # ?
  • Can be Null.

Note:

  • The extendInfo.appCustomerId assigned by the super app is passed to this field to identify a user.
  • The extendInfo.acqCustomerIdassigned by Alipay merchant service is also passed to this field to uniquely identify a user.

See Response sample for details.

Result process logic

In the response, the result.resultStatus field indicates the result of processing a request. The following table describes each result status:

Result status

Description 

S

The authorization request is successful.

The corresponding result.resultCode is SUCCESS and the result.resultMessage is SUCCESS.

U

The status of the authorization request is unknown.  

The corresponding result.resultCode is UNKNOWN_EXCEPTION and result.resultMessage is "An API calling is failed, which is caused by unknown reasons.".

For details, see the Common error codes section.

F

The authorization request is failed.

The corresponding result.resultCode and result.resultMessage are various based on different situations. For details, see the following Error codes section.

Error codes

Error codes are usually classified into the following categories:

  • Common error codes are common for all mini program OpenAPIs in V2.  
  • API-specific error codes are listed in the following table.

Error code

Result status

Error message

Further action

AUTH_CLIENT_UNSUPPORTED_GRANT_TYPE

F

The authorized merchant does not support this grant type.

Use a valid grantType such as AUTHORIZATION_CODE or

REFRESH_TOKEN.

INVALID_AUTH_CLIENT

F

Either the authorized merchant does not exist or the merchant does not onboard to the native app.

Use a valid authClientId assigned by the super app.

INVALID_AUTH_CLIENT_STATUS

F

The status of the authorized merchant is invalid.

Contact technical support to troubleshoot the issue.

INVALID_REFRESH_TOKEN

F

The refresh token does not exist.

Obtain a new refresh token via this API.

EXPIRED_REFRESH_TOKEN

F

The refresh token expires.

Obtain a new authorization code from the super app via the my.getAuthCode JSAPI and then obtain a new refresh token via this API.

USED_REFRESH_TOKEN

F

The refresh token has been used.

Obtain a new refresh token via this API.

INVALID_AUTHCODE

F

The authorization code does not exist.

Obtain a new authorization code from the super app via the my.getAuthCode JSAPI.

USED_AUTHCODE

F

The authorization code has been used.

Obtain a new authorization code from the super app via the my.getAuthCode JSAPI.

EXPIRED_AUTHCODE

F

The authorization code expires.

Obtain a new authorization code from the super app via the my.getAuthCode JSAPI.

Samples

The data flow to obtain an access token is illustrated as below:

image

  1. The mini program calls the my.getAuthCode JSAPI to request an authorization code from the super app.
  2. The super app processes the request and shows the information that needs to be authorized.
  3. The user confirms the authorization in the super app.
  4. The super app service processes the authorization information to the super app server.
  5. The super app server verifies the authorization information and then generates the authorization code.
  6. The super app server returns the authorization code to the super app service.
  7. The super app service returns the authorization code to the mini program.
  8. The mini program sends the authorization code to the merchant server.
  9. The merchant server calls this API to exchange for an access token from the super app server.
  10. The super app server verifies the authorization code and generates the access token.
  11. The super app returns the access token to the merchant server.

Request

  • Use an authorization code to exchange for an access token
copy
{
  "appId":"3333010071465913xxx",
  "authClientId": "202016726873874774774xxxx",
  "grantType": "AUTHORIZATION_CODE",
  "authCode": "2810111301lGZcM9CjlF91WH00039190xxxx"
}

The mini program (3333010071465913xxx) calls the my.getAuthCode JSAPI to obtain the authorization code (2810111301lGZcM9CjlF91WH00039190xxxx) and then send the authorization code to the merchant (202016726873874774774xxxx). The merchant uses the authorization code to exchange for an access token as grantType is AUTHORIZATION_CODE.

  • Use a refresh token to exchange for an access token
copy
{
  "grantType": "REFRESH_TOKEN",
  "refreshToken": "2810111301lGZcM9CjlF91WH00039190xxxx"
}

The value of grantType is REFRESH_TOKEN, which means the merchant can obtain an access token by the refresh token (2810111301lGZcM9CjlF91WH00039190xxxx).

Response

copy
{
    "result": {
        "resultCode": "SUCCESS",
        "resultStatus": "S",
        "resultMessage": "success"
    },
    "accessToken": "281010033AB2F588D14B43238637264FCA5AAF35xxxx",
    "accessTokenExpiryTime": "2019-06-06T12:12:12+08:00",
    "refreshToken": "2810100334F62CBC577F468AAC87CFC6C9107811xxxx",
    "refreshTokenExpiryTime": "2019-06-08T12:12:12+08:00",
    "customerId": "1000001119398804xxxx",
    "extendInfo": "{\"appCustomerId\":\"200xxxx\",\"acqCustomerId\":\"300xxxx\"}"
}
  • result.resultStatus is S, which shows the request to obtain an access token is successful.
  • The authorized merchant can use the access token (281010033AB2F588D14B43238637264FCA5AAF35) before accessTokenExpiryTime (2019-06-06T12:12:12+08:00).
  • 1000001119398804xxxx is the user who authorizes the merchant.
  • The authorized merchant can use the refresh token (2810100334F62CBC577F468AAC87CFC6C9107811) to exchange for a new access token before refreshTokenExpiryTime (2019-06-08T12:12:12+08:0).
  • extendInfo returns another two IDs to identify the user:
    • appCustomerId: Indicates the ID assigned by the super app to identify a user.
    • acqCustomerId: Indicates the ID assigned by Alipay merchant service to identify a user.

Related links

my.getAuthCode

my.getAppIdSync