/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 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"

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"

Response parameters

Field

Data type

Required

Description

Example

result

Result

Yes

Indicates the request result such as status and error codes.

{

"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

Long

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 would be the timestamp.

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

1682058090533

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 would be the timestamp.

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

1682058090533

userId

String

Yes

UserId

"1000001119398804xxxx"

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

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_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.

INVALID_AUTHCODE

F

The authorization code does not exist.

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
{
  "appId":"3333010071465913xxx",
  "authClientId": "202016726873874774774xxxx",
  "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": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzZXNzaW9uS2V5IjoiM2U5YmMzZmItNmI0Yy00MzA1LTlhZTgtNGJlNmRiOTU0NTE2IiwidXNlcl9uYW1lIjoiVXNlclByb2ZpbGVWbyhpZD0yMDQzLCB0ZW5hbnRJZD0xLCB1c2VySWQ9MjA0Mywg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• 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 (2019-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 (2019-06-08T12:12:12+08:0).

Related links

my.getAuthCode

my.getAppIdSync