my.getOpenUserInfo

Get the basic information about a user. This feature requires the user to deliberately trigger to activate the function. This function is not directly called by the API but rather waits for when the user has activated it by clicking a <button> component. If the Mini Program wants to get userId, please call my.getAuthCode.
For more information, please refer to the obtain basic member information.

Use Attention

You need to set the value of the <button> component open-type to getAuthorize and set the value of the scope to userInfo . After the user clicks the authorization button, the Mini Program can get the user information returned by the my.getOpenUserInfo JSAPI.
my.getOpenUserInfo will send a network request to the server to obtain user information, so it may be take some time before the callback function invoked.

Sample

The data flow to obtain an access token is illustrated as below:

my.getOpenUserInfo.png

  1. The Mini Program calls this my.getOpenUserInfo JSAPI with specific scope usrInfo to request an authorization from user.
  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, and returns the user basic information.
  6. The E-wallet App service returns the user basic information to the Mini Program.

Parameters

NameType

Required

Description
success FunctionNoCallback function upon call success.
failFunctionNoCallback function upon call failure.
completeFunctionNoCallback function upon call completion (to be executed upon either call success or failure).

Success Callback Function

The incoming parameter is of the Object type with the following attributes:

NameType

Required

Description
codeStringNOThe result code.
msgStringNOThe result message.
avatarStringNOUser avatar image url.
genderStringNOUser gender. "M" is male,"F" is female.
nationalityStringNOThe code of the country where user is located. it should follow ISO 3166-1 alpha-2 code standard, such as 'US', 'SG'.

These fields are returned every time, but it will be an empty string if the app does not return the related information.
The maximum length of these fields are 128 bytes except avatar,the maximum length of avatar is 2048 bytes.

Sample Code

copy
<!-- .axml -->
<button 
    a:if="{{canIUseAuthButton}}" 
    open-type="getAuthorize" 
    onGetAuthorize="onGetAuthorize" 
    onError="onAuthError" 
    scope='userInfo'>
</button>

Button Property Description

NameDescription
open-typegetAuthorize(Must be this value). 
scopeuserInfo(Must be this value).
onGetAuthorizeAuthorization success callback (The Mini Program can call my.getOpenUserInfo to get information in this callback).
onErrorAuthorization failure callback (Including user rejection and system exceptions).

Get User Basic Information

After the user clicks the consent, the user basic information can be obtained through my.getOpenUserInfo().

copy
// .js 
onGetAuthorize(res) {
    my.getOpenUserInfo({
        fail: (res) => {
        },
        success: (res) => {
            let userInfo = JSON.parse(res.response).response
        }
    });
}

Return Res Object In the Success Callback

  • An example of a successfully res object returned is as follows:
copy
{
  "response": "{\"response\":{\"code\":\"10000\",\"msg\":\"Success\",\"avatar\":\"https://cdn/images/partner/XXXXXXXX\",\"gender\":\"F\",\"nationality\":\"US\"}}"
}
  • If the function package of "Get Basic User Information" is not connected, the format example of returned res message is as follows:
copy
{
  "response":"{\"response\":{\"code\":\"40006\",\"msg\":\"Insufficient Permissions\",\"subCode\":\"isv.insufficient-isv-permissions\",\"subMsg\": \"Insufficient permissions\"}}"
}