目录

1. Purpose

This document is applicable to developers who want to connect their Android apps to HUAWEI Account Kit. HUAWEI Account Kit provides the HMS SDK for quick access. However, some of you may not want to use Huawei Mobile Services (APK). In this case, you can use OAuth 2.0 to enable users to sign in to your apps using their HUAWEI IDs in browsers. To quickly access OAuth 2.0 for sign-in authorization, the AppAuth SDK is required.
The AppAuth SDK on the client is used for communication through OAuth 2.0. It sends requests and gives responses according to the protocol and complies with the common style of the development language you use. It also supports extension on the Proof Key for Code Exchange (PKCE) of OAuth to protect authorization codes on the client.

2. Configuration Requirements

3. Preparations

3.1 Obtaining the App ID

Before accessing HUAWEI Account Kit, create an app and obtain the app ID (passed as the value of client_id during the access).

4. Development Process

4.1 Obtaining Sample Code

Download sample code from AppAuth For Android or run the following command to clone the code:

  1. git clone https://github.com/openid/AppAuth-Android.git

Start the project in Android Studio.

4.2 Setting Request Parameters

During the authorization, parameters such as client_id and the authorization address are required. Set them in the /res/raw/auth_config.json configuration file. The following table describes the parameters to be used.

ParameterMandatory (M)/Optional (O)Example ValueDescription
client_idMAPPIDApp ID you applied for in HUAWEI Developer. For details, see section 3.1 Obtaining the App ID.
redirect_uriMcom.huawei.apps.APPID:/oauth2redirectThe value is in a fixed format, in which APPID indicates the app ID applied for in HUAWEI Developer. For details, see section 3.1 Obtaining the App ID.
authorization_scopeMopenid email profileList of permissions granted by users. In the value, openid is mandatory. If you need to set multiple scopes here, separate them by spaces.
discovery_uriOhttps://oauth-login.cloud.huawei.com/.well-known/openid-configurationURI for dynamic discovery address of the OpenID-based authorization service. If this parameter has been set, authorization_endpoint_uri and token_endpoint_uri are optional.
authorization_endpoint_uriOhttps://oauth-login.cloud.huawei.com/oauth2/v3/authorizeURI from which an authorization request is obtained. This parameter is mandatory if discovery_uri is not set.
token_endpoint_uriOhttps://oauth-login.cloud.huawei.com/oauth2/v3/tokenURI from which a token is obtained. This parameter is mandatory if discovery_uri is not set.
https_requiredMtrueIndicates whether to use HTTPS requests. The value is true for sign-in using a HUAWEI ID.

4.3 Configuring a Redirection Address

In the app-level .gradle file of the sample code obtained, find appAuthRedirectScheme and change its value to com.huawei.apps.APPID. Replace APPID with the App ID you applied for in HUAWEI Developer.
Example:

  1. manifestPlaceholders = [
  2.       'appAuthRedirectScheme': 'com.huawei.apps.{APPID}'
  3. ]

4.4 Executing the Sample Code

After setting the request parameters, execute the sample code obtained. Select a browser and click START AUTHORIZATION. The browser page is displayed, on which the user will see the sign-in authorization page.
After the user signs in with a HUAWEI ID in the browser and the authorization is successful, you can obtain authorization information such as the ID token and access token.

4.5 Parsing the ID Token to Obtain User Information

The returned ID token is a character string in JWT format. It can be parsed in JWT format to obtain the Union ID, email address, and avatar of the user.

For details about how to verify the ID token, please refer to Verifying ID Token Validity. For details about the parameters parsed from the ID token, please refer to Verify ID Token.

5. Related Documents