/v1/authorizations/applyToken

POST /v1/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

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"

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

Resource owner ID, which can be user ID, app ID of merchant's application, or merchant ID.

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

"1000001119398804xxxx"

extendInfo

String

No

Indicates the extended information about this API.

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

N/A

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 V1.
  • 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_REFRESH_TOKEN

F

The refresh token is invalid.

Obtain a new refresh token via this API.

EXPIRED_REFRESH_TOKEN

F

The refresh token is expired.

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_CODE

F

The authorization code is invalid.

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

USED_CODE

F

The authorization code has been used.

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

EXPIRED_CODE

F

The authorization code is expired.

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:

applyToken.png

  1. The Mini Program calls the my.getAuthCode JSAPI with specific scopes to request an authorization code.
  2. The E-wallet App processes the request and displays the authorization page that needs to be authorized.
  3. The user confirms the authorization in the super app.
  4. The E-wallet App service calls authorization service to processes the authorization information.
  5. The E-wallet backend verifies the authorization information, generates the authCode and returns.
  6. The E-wallet App service returns the authCode to the Mini Program.
  7. The Mini Program sends the authCode to the merchant backend.
  8. The merchant backend calls the applyToken API with authCode to apply the accessToken.
  9. The E-Wallet backend returns accessToken information to the merchant backend, such as accessToken, refreshToken, etc.

Request

  • Use an authorization code to exchange for an access token
copy
{
  "grantType": "AUTHORIZATION_CODE",
  "authCode": "2810111301lGZcM9CjlF91WH00039190xxxx"
}

The mini program calls the my.getAuthCode JSAPI to obtain the authorization code (2810111301lGZcM9CjlF91WH00039190xxxx) and then send the authorization code to the merchant. 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": "2023-06-06T12:12:12+08:00",
    "refreshToken": "2810100334F62CBC577F468AAC87CFC6C9107811xxxx",
    "refreshTokenExpiryTime": "2023-06-08T12:12:12+08:00",
    "customerId": "1000001119398804xxxx"
}
  • result.resultStatus is S, which shows the request to obtain an access token is successful.
  • The authorized merchant can use the access token (281010033AB2F588D14B43238637264FCA5AAF35xxxx) before accessTokenExpiryTime (2023-06-06T12:12:12+08:00).
  • 1000001119398804xxxx is the user who authorizes the merchant.
  • The authorized merchant can use the refresh token (2810100334F62CBC577F468AAC87CFC6C9107811xxxx) to exchange for a new access token before refreshTokenExpiryTime (2023-06-08T12:12:12+08:0).

Related links

my.getAuthCode