/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.
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.
| "202016726873874774774xxxx" |
grantType | String | Yes | Indicates the way the authorized merchant obtains an access token. Valid values are:
| "AUTHORIZATION_CODE" |
customerBelongsTo | String | No | Indicates the super app that a user uses. Valid values are:
| "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.
Note: This field is required when the value of grantType is | "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.
Note: This field is required when the value of grantType is | "2810111301lGZcM9CjlF91WH00039190xxxx" |
extendInfo | String | No | Indicates the extended information about this API.
| copy
|
Response parameters
Field | Data type | Required | Description | Example |
result | Yes | Indicates the request result such as status and error codes. | copy
| |
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.
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.
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.
| "1000001119398804xxxx" |
extendInfo | String | No | Indicates the extended information about this API.
| 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 |
U | The status of the authorization request is unknown. The corresponding result.resultCode is 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Â
|
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:
- The mini program calls the my.getAuthCode JSAPI to request an authorization code from the super app.
- The super app processes the request and shows the information that needs to be authorized.
- The user confirms the authorization in the super app.
- The super app service processes the authorization information to the super app server.
- The super app server verifies the authorization information and then generates the authorization code.
- The super app server returns the authorization code to the super app service.
- The super app service returns the authorization code to the mini program.
- The mini program sends the authorization code to the merchant server.
- The merchant server calls this API to exchange for an access token from the super app server.
- The super app server verifies the authorization code and generates the access token.
- The super app returns the access token to the merchant server.
Request
- Use an authorization code to exchange for an access token
{
"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
{
"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
{
"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"
}
- 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
).