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.
A browser has been installed on the Android device.
Before accessing HUAWEI Account Kit, create an app and obtain the app ID (passed as the value of client_id during the access).
Download sample code from AppAuth For Android or run the following command to clone the code:
- git clone https://github.com/openid/AppAuth-Android.git
Start the project in Android Studio.
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.
| Parameter | Mandatory (M)/Optional (O) | Example Value | Description |
|---|---|---|---|
| client_id | M | APPID | App ID you applied for in HUAWEI Developer. For details, see section 3.1 Obtaining the App ID. |
| redirect_uri | M | com.huawei.apps.APPID:/oauth2redirect | The 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_scope | M | openid email profile | List of permissions granted by users. In the value, openid is mandatory. If you need to set multiple scopes here, separate them by spaces. |
| discovery_uri | O | https://oauth-login.cloud.huawei.com/.well-known/openid-configuration | URI 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_uri | O | https://oauth-login.cloud.huawei.com/oauth2/v3/authorize | URI from which an authorization request is obtained. This parameter is mandatory if discovery_uri is not set. |
| token_endpoint_uri | O | https://oauth-login.cloud.huawei.com/oauth2/v3/token | URI from which a token is obtained. This parameter is mandatory if discovery_uri is not set. |
| https_required | M | true | Indicates whether to use HTTPS requests. The value is true for sign-in using a HUAWEI ID. |
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:
- manifestPlaceholders = [
- 'appAuthRedirectScheme': 'com.huawei.apps.{APPID}'
- ]
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.
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.