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:

my.getAuthCode.png

  1. The Mini Program calls this 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

copy
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:

  • auth_base
  • auth_user
  • NOTIFICATION_INBOX 

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. key is the scope and value is the error.

authSuccessScopes

Array

No

The scope that succeeds to grant authorization.

Success Callback Function

copy
{
    "authCode":"0000000001T1dc0W0tv448DB00007826"
}

Fail Callback Function

copy
{
    "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 CodeDescription
3ERROR_CODE_UNKNOWN_ERROR
1000ERROR_CODE_UNKNOWN_ERROR
1001ERROR_CODE_USER_CANCEL
1002ERROR_CODE_APP_SERVICE_ERROR
1003ERROR_CODE_TIMEOUT
2001ERRORLCODE_AUTH_PENDING_AGREEMENT