目录

Account Kit

  • Before using this API, complete operations in Applying for the MCP Permission for an Account.
  • This function depends on the services provided by the Account SDK. The SDK may collect personal data of users. Before using this function in a quick app, inform the users that personal data will be collected and guide them to read the privacy statement.

This document describes only the definition of APIs. For details about how and when to call these APIs, please refer to Integrating Account Kit.

API Declaration

Add the following configuration to the features attribute in the manifest.json file:

{"name": "service.account"}

Module Import

Add the following configuration to the script element on the page where the API will be called:

import account from '@service.account'

Or

var account = require("@service.account")

Application Scope

Item

Description

Applicable devices

Mobile phones, tablets, Vision products, and telematics devices

Applicable regions

Global

Definition

API

Description

account.getProvider(OBJECT)

Obtains the service provider, for example, the English brand name of the vendor.

account.authorize(OBJECT)

Performs OAuth authorization.

account.getProfile(OBJECT)

Obtains basic user information.

account.checkUserSession(OBJECT)

Checks whether an app has been authorized for sign-in through the authorize API and whether the account has been signed out after sign-in.

account.isLogin(OBJECT)

Checks whether a HUAWEI ID has been signed in. This API is not supported on head units.

account.signOut(OBJECT)

Clears the cache when the access token obtained by account.authorize fails to be used to call the account.getProfile API (result code: 102). After the cache is cleared, account.authorize is called again to obtain a new access token. This API is supported only on head units.

[h2]account.getProvider(OBJECT)

Description

Obtains the service provider, for example, the English brand name of the vendor.

Returns

A service provider identifier is returned, which is a character string. If the service does not exist, an empty string is returned. If so, do not call other APIs of the service. Otherwise, an exception will occur.

Sample code

account.getProvider();

[h2]account.authorize(OBJECT)

Description

Performs OAuth authorization.

OBJECT parameters

Parameter

Type

Mandatory/Optional

Description

appid

string

Mandatory

Unique ID allocated to a quick app created on HUAWEI Developers (not formulated by the vendor alliance). For details about how to obtain the ID, please refer to Obtaining the App ID.

type

string

Mandatory

token: simplified mode

code: authorization code mode

redirectUri

uri

Optional

Redirection URI.

scope

string

Mandatory

Requested permission scope.

scope.baseProfile: obtains basic user information

forceLogin(1079+)

string

Optional

Indicates whether to open the sign-in screen when a user does not sign in using a HUAWEI ID or the authentication fails.

The default value is 1.

  • 0: no
  • 1: yes

state

string

Optional

Any value defined by you. Huawei OAuth server returns the value without any change.

success

function

Optional

Callback upon success.

fail

function

Optional

Callback upon failure.

complete

function

Optional

Callback upon completion.

Parameters returned in success

Parameter

Type

Description

state

string

Value defined in the request.

code

string

Authorization code. This parameter is applicable only to the authorization code mode.

accessToken

string

Access token. This parameter is applicable only to the implicit mode.

tokenType

string

Access token type. This parameter is applicable only to the implicit mode. You can use this token to call the getProfile API to obtain basic user information and OpenID.

expiresIn

number

Expiration time of the access token, in seconds. This parameter is applicable only to the implicit mode and is empty in the authorization code mode.

scope

string

Scope of granted permissions. This parameter is applicable only to the implicit mode and is empty if the value in the request is used.

openid (1020+)

string

OpenID of a user. This parameter is applicable only to the implicit mode.

unionid (1020+)

string

Unique ID of a user. This parameter is returned only after Enable UnionID is selected on HUAWEI Developers when Account Kit is applied for, and is applicable only to the implicit mode.

nickname (1020+)

string

User nickname. This parameter is applicable only to the implicit mode and can be empty.

avatar (1020+)

object

Profile picture URI of a user. This parameter is applicable only to the implicit mode and can be empty. If only one resolution is available, the default URI is used.

email (1060+)

string

Email address of a user. This parameter is applicable only to the implicit mode. If no email address is bound, this parameter may be empty.

Result codes returned in fail

Result Code

Description

201

The user has rejected the request of obtaining the account.

Sample code

For implicit mode

account.authorize({
    appid: "101373095",
    type: "token",
    state: "feedbeef",
    scope: "scope.baseProfile",
    success: function (data) {
        console.log("signInByBaseScope Authorize base profile success.");
        var accessToken = data.accessToken;
        console.log("signInByBaseScope Authorize base profile success data=" + JSON.stringify(data));
    },
    fail: function (data, code) {
        console.log("signInByBaseScope Authorize base profile fail, code=" + code);
        prompt.showToast({ message: data + "fail code: " + code })
    }
});

For authorization code mode

account.authorize({
  appid: "101373095",
  type: "code",
  state: 5,
  scope: "scope.baseProfile",
  redirectUri: "https://www.example.com/",
  success: function (ret) {
    var atcode = ret.code;
    console.log("signInByBaseScope Authorize base profile success data=" + JSON.stringify(ret));
  },
  fail: function (erromsg, errocode) {
    prompt.showToast({ message: 'authorize fail --- ' + errocode + ':' + erromsg });
  }
})

[h2]account.getProfile(OBJECT)

Description

Obtains basic user information.

OBJECT parameters

Parameter

Type

Mandatory/Optional

Description

appid

string

Optional

Unique ID allocated to a quick app created on HUAWEI Developers (not formulated by the vendor alliance). For details about how to obtain the ID, please refer to Obtaining the App ID.

token

string

Mandatory

Access token obtained by calling account.authorize.

success

function

Optional

Callback upon success.

fail

function

Optional

Callback upon failure.

complete

function

Optional

Callback upon completion.

Parameters returned in success

Parameter

Type

Description

openid

string

OpenID of a user.

id

string

Huawei does not return this parameter, but other vendors may return it.

unionid

string

Unique ID of a user. This parameter is returned only after Enable UnionID is selected on HUAWEI Developers when Account Kit is applied for.

nickname

string

User nickname. This parameter is optional.

avatar

object

Profile picture URI of a user. This parameter is optional. If only one resolution is available, the default URI is used.

Result codes returned in fail

Result Code

Description

102

The session is invalid.

UnionID mechanism

If you have multiple mobile apps, a user of your apps can be uniquely specified by UnionID. That is, a user has only one UnionID across all apps on your open platform.

Sample code

account.getProfile({
 appid: "101373095",
    token: this.accessToken,       // Use the actual access token.
    success: function (data) {
        console.log('getProfile handling success: ' + JSON.stringify(data));
    },
    fail: function (data, code) {
        console.log('getProfile handling fail, code =' + code);
    }
})

[h2]account.checkUserSession(OBJECT) (1020+)

Description

Checks whether an app has been authorized for sign-in through the authorize API and whether the account has been signed out after sign-in. The verification fails in the following scenarios:

If you want your app to share the sign-in and sign-out states with the user's HUAWEI ID, call account.checkUserSession in authorization code mode. If the authorization code is invalid, call account.authorize again.

The account.checkUserSession API does not need to be called for verification in implicit mode.

OBJECT parameters

Parameter

Type

Mandatory/Optional

Description

success

function

Optional

Callback upon success.

fail

function

Optional

Callback upon failure.

Sample code

account.checkUserSession({
    success: function (data2) {
        // The verification is successful.
    },
    fail: function (data, code) {
        // The verification fails.
    }
});

[h2]account.isLogin(OBJECT) (1050+)

Description

Checks whether a HUAWEI ID has been signed in. This API is not supported on head units.

OBJECT parameters

Parameter

Type

Mandatory/Optional

Description

success

function

Optional

Callback upon success.

fail

function

Optional

Callback upon failure.

Parameters returned in success

Parameter

Type

Description

isLogin

boolean

Sign-in status. The value true indicates that the user has signed in, and false indicates that the user has not signed in.

Result codes returned in fail

Result Code

Description

200

Invalid common parameter.

Sample code

account.isLogin({
    success:function(data){
        console.log("handling success,  data.isLogin= " + data.isLogin);
    },
    fail: function(data, code) {
        console.log(`handling fail, code = ${code}`);
    }
})

[h2]account.signOut(OBJECT)

Description

Clears the cache when the access token obtained by account.authorize fails to be used to call the account.getProfile API (result code: 102). After the cache is cleared, account.authorize is called again to obtain a new access token. This API is supported only on head units.

OBJECT parameters

Parameter

Type

Mandatory/Optional

Description

appid

string

Mandatory

App ID obtained from HUAWEI Developers after creating an app.

success

function

Optional

Callback upon success.

fail

function

Optional

Callback upon failure.

Result codes returned in fail

Result Code

Description

200

General error.

202

Invalid parameter.

Sample code

account.signOut({
    appid: "123456",
    success:function(data){
        console.log("handling success");
    },
    fail: function(data, code) {
        console.log(`handling fail, code = ${code}`);
    }
})

Version Change History

Version

Date

Description

1050

2019-09-20

Added the account.isLogin API to check whether a HUAWEI ID has been signed in.

1020

2018-06-20

  • Added the openid, unionid, nickname, and avatar values returned by the account.authorize API.
  • Added the account.checkUserSession API to check whether the sign-in status of a user is valid.

Links

[h2]FAQs

What can I do if result code 6200 is returned when account.getProfile is called?