This document describes only the definition of APIs. For details about how and when to call these APIs, please refer to Integrating Account Kit.
Add the following configuration to the features attribute in the manifest.json file:
{"name": "service.account"} 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") Item | Description |
|---|---|
Applicable devices | Mobile phones, tablets, Vision products, and telematics devices |
Applicable regions | Global |
API | Description |
|---|---|
Obtains the service provider, for example, the English brand name of the vendor. | |
Performs OAuth authorization. | |
Obtains basic user information. | |
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. | |
Checks whether a HUAWEI ID has been signed in. This API is not supported on head units. | |
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. |
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();
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.
|
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 });
}
}) 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);
}
}) 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.
}
}); 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}`);
}
}) 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 | 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 |
|