my.getAuthCode
Call the API to obtain the authorization code. The authorization code can be used to obtain the access token, so as to obtain the app user's information, such as user ID and nickname.
For more information, refer to User authorization.
Sample
The data flow to obtain an access token is illustrated as below:
- The Mini Program calls this my.getAuthCode JSAPI with specific scopes to request an authorization code.
- The E-wallet App processes the request and displays the authorization page that needs to be authorized.
- The user confirms the authorization in the super app.
- The E-wallet App service calls authorization service to processes the authorization information.
- The E-wallet backend verifies the authorization information, generates the authCode and returns.
- The E-wallet App service returns the authCode to the Mini Program.
- The Mini Program sends the authCode to the merchant backend.
- The merchant backend calls the applyToken API with authCode to apply the accessToken.
- The E-Wallet backend returns accessToken information to the merchant backend, such as accessToken, refreshToken, etc.
Request
my.getAuthCode({
scopes: ['auth_base'],
success: (res) => {
my.alert({
content: res.authCode,
});
},
fail: (res) => {
console.log(res.authErrorScopes)
},
});
Parameters
Property | Type | Required | Description |
scopes | Array | Yes | The scope of authorization, including:
|
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
more details: User authorization
Scopes | Description | Silent authorization |
auth_base | Authorized to obtain the unique user ID. | Yes |
USER_ID | Authorized to obtain the unique user ID. | Yes |
USER_LOGIN_ID | Authorized to obtain the user's login ID. | Yes |
HASH_LOGIN_ID | Authorized to obtain the hash user login ID. | Yes |
auth_user | Authorized to obtain the basic user information. | No |
USER_NAME | Authorized to obtain the user name. | No |
USER_AVATAR | Authorized to obtain the user avatar. | No |
USER_GENDER | Authorized to obtain the user's gender. | No |
USER_BIRTHDAY | Authorized to obtain the user's birthday. | No |
USER_NATIONALITY | Authorized to obtain the user's nationality. | No |
USER_CONTACTINFO | Authorized to obtain the user's contact information. | No |
NOTIFICATION_INBOX | Authorized to send notification to user's notification in app inbox. | No |
AGREEMENT_PAY | Authorized to auto-debit payment from user's balance or card. | No |
Note: auth_base
is used to silently obtain user ID only. Silent authorization does not pop the frame and directly obtains user information which means it doesn't require user's authorization. All the other scopes are used for proactive user authorization.
Callback function
The incoming parameter is of the Object type with the following attributes:
Property | Type | Required | Description |
authCode | String | Yes | Authorization code. |
authErrorScopes | Key-value | No | The scope that fails to grant authorization. |
authSuccessScopes | Array | No | The scope that succeeds to grant authorization. |
Success Callback Function
{
"authCode":"0000000001T1dc0W0tv448DB00007826"
}
Fail Callback Function
{
"authErrorScopes":{
"auth_user":"10001"
}
}
Error Code
When error happens, the fail callback function will be executed. The error code can refer to the following table.
Error Code | Description |
3 | ERROR_CODE_UNKNOWN_ERROR |
1000 | ERROR_CODE_UNKNOWN_ERROR |
1001 | ERROR_CODE_USER_CANCEL |
1002 | ERROR_CODE_APP_SERVICE_ERROR |
1003 | ERROR_CODE_TIMEOUT |
2001 | ERRORLCODE_AUTH_PENDING_AGREEMENT |