Huawei Account Kit Flutter Plugin

Table Of Contents

  • Introduction
  • Installation Guide
  • API Reference
  • Configuration Description
  • Licensing and Terms

Introduction

This plugin enables communication between Huawei Account SDK and Flutter platform. It enables user login processes to be carried out quickly and easily with HUAWEI Account Kit's two factor authentication.

Installation Guide

  • Before you get started, you must register as a HUAWEI developer and complete identity verification on the HUAWEI Developer website. For details, please refer to Register a HUAWEI ID.

  • Create an app by referring to Creating an AppGallery Connect Project and Adding an App to the Project.

  • A signing certificate fingerprint is used to verify the authenticity of an app when it attempts to access an HMS Core (APK) through the HMS SDK. Before using the HMS Core (APK), you must locally generate a signing certificate fingerprint and configure it in AppGallery Connect. For details, please refer to Generating a Signing Certificate.

  • Sign in to AppGallery Connect and select My apps. Then, on the Project Setting page, set SHA-256 certificate fingerprint to the SHA-256 fingerprint from Configuring the Signing Certificate Fingerprint.

  • In AppGallery Connect, on My apps page, find your app from the list and click the app name. Go to Development > Overview > App Information. Click agconnect-service.json to download configuration file.

  • Copy the agconnect-service.json file to the android/app directory of your project.

  • Open the build.gradle file in the android directory of your project.

    • Go to buildscript then configure the Maven repository address and agconnect plugin for the HMS SDK.

        buildscript {
            repositories {
                google()
                jcenter()
                maven { url 'https://developer.huawei.com/repo/' }
            }
      
            dependencies {
                /*
                 * <Other dependencies>
                 */
                classpath 'com.huawei.agconnect:agcp:1.3.1.300'
            }
        }
      
    • Go to allprojects then configure the Maven repository address for the HMS SDK.

        allprojects {
            repositories {
                google()
                jcenter()
                maven { url 'https://developer.huawei.com/repo/' }
            }
        }
      
  • Open the build.gradle file in the android/app directory.

    • Add apply plugin: 'com.huawei.agconnect' line after the apply plugin: 'com.android.application' line.

        apply plugin: 'com.android.application'
        apply plugin: 'com.huawei.agconnect'
        apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
      
    • Set your package name in defaultConfig > applicationId and set minSdkVersion to 19 or higher. Package name must match with the package_name entry in agconnect-services.json file.

        defaultConfig {
                applicationId "<package_name>"
                minSdkVersion 19
                /*
                 * <Other configurations>
                 */
            }
      
    • Copy the signature file that generated in Generating a Signing Certificate to android/app directory.

    • Configure the signature in android according to the signature file information.

        android {
            /*
             * <Other configurations>
             */
      
            signingConfigs {
                config {
                    storeFile file('<keystore_file>.jks')
                    storePassword '<keystore_password>'
                    keyAlias '<key_alias>'
                    keyPassword '<key_password>'
                }
            }
      
            buildTypes {
                debug {
                    signingConfig signingConfigs.config
                }
                release {
                    signingConfig signingConfigs.config
                }
            }
        }
      
  • On your Flutter project directory find and open your pubspec.yaml file and add library to dependencies. For more details please refer the Using packages document.

      dependencies:
          huawei_account:
              # Replace {library path} with actual library path of Huawei Account Kit Flutter Plugin.
              path: {library path}
    
  • Run following command to update package info.

      [project_path]> flutter pub get
    
  • Run following command to start the app.

      [project_path]> flutter run
    

API Reference

This section does not cover all of the API, to read more, visit HUAWEI Developer website.

Hms Account

Methods

Return TypeMethodDescription
Future<AuthHuaweiId>signIn(AuthParamHelper authParamHelper)Obtains the Intent of the authorization page by using authParamHelper object
Future<AuthHuaweiId>signInWithAuthorizationCode(AuthParamHelper authParamHelper)Obtains the Intent of the authorization page by using authParamHelper object
Future<AuthHuaweiId>silentSignIn(AuthParamHelper authParamHelper)Performs a silent authorization, no authorization page will be shown.
Future<bool>signOut()This API is called to sign out of a HUAWEI ID from an app.
Future<bool>revokeAuthorization()Cancels the authorization. Users will be redirected to authorization page while resigning in.
Future<String>obtainHashCode()Obtains the applications hash code which is unique in each app
FuturesmsVerification(SmsListener listener)Starts listening upcoming messages that contains app's hashcode
typedef voidSmsListener({String message, String errorCode})A function type defined for listening messages. Returns the message or the errorCode through smsVerification() method.
Future<AuthHuaweiId>getAuthResult()Obtains the latest authorization information.
Future<AuthHuaweiId>getAuthResultWithScopes(List<String> scopeList)Obtains the AuthHuaweiId instance with parameter that List object.
Future<bool>containScopes(Map<String, dynamic> authData, List<String> scopeList)Checks whether the user with the designated HUAWEI ID has been assigned all permissions specified by scopeList.
Future<bool>addAuthScopes(int requestCode, List<String> scopeList)Requests the permission specified by scopeList from a HUAWEI ID.
Future<bool>deleteAuthInfo(String accessToken)Clears the local cache.
Future<String>requestUnionId(String huaweiAccountName)Obtains a union id.
Future<String>requestAccessToken(Account account, List<String> scopeList)Obtains an access token.
Future<String>buildNetworkUrl(String domainName, bool isHttps)Returns cookie url based on the domain name and isHttps.
Future<String>buildNetworkCookie(String cookieName, String cookieValue, String domain, String path, bool isHttpOnly, bool isSecure, double maxAge)Constructs a cookie by combining input values.

Data Types

AuthHuaweiId
PropertiesTypeDescription
accountAccountObtains the account information that contains type and name values.
accessTokenStringObtains the access token
displayNameStringObtains the display name
emailStringObtains the email
familyNameStringObtains the family name
givenNameStringObtains the given name
idTokenStringObtains the id token
authorizationCodeStringObtains the authorization code
unionIdStringObtains the union id
avatarUriStringStringObtains the profile picture url
openIdStringObtains the open id
uidStringObtains the uid
countryCodeStringRegistration country code
serviceCountryCodeStringService country code
authorizedScopesList<String>Obtains the authorized scopes
statusintUser status. 1: Normal; 2: Dbank suspended; 3: Deregistered, 4: All services are suspended.
genderintUser Gender. -1: Unknown, 0: male, 1: female, 2: confidential.
AuthParams
PropertiesTypeValueDescription
defaultAuthRequestParamint0Default authorization parameter of HUAWEI ID.
defaultAuthRequestParamGameint1Default authorization parameter of game applications.
AuthParamHelper
PropertiesTypeDescription
authParamHelpersMap<String, dynamic>This property is used in signIn methods to customize authorization.
MethodsReturn TypeDescription
setDefaultParam(int defaultParam)voidAllows you to choose an AuthParams type
setRequestCode(int requestCode)voidAllows you to choose a request code.
setIdToken()voidRequests a HUAWEI ID user to authorize ID token to an app.
setProfile()voidRequests a HUAWEI ID user to authorize the profile information to an app.
setAccessToken()voidRequests a HUAWEI ID user to authorize Access Token to an app.
setAuthorizationCode()voidRequests a HUAWEI ID user to authorize Authorization Code to an app.
setEmail()voidRequests a HUAWEI ID user to authorize the email address to an app.
setId()voidRequests a HUAWEI ID user to authorize unionId to an app.
addToScopeList(List<String> scopelist)voidAdds a specified Scope to the authorization configuration.
Scope
PropertiesTypeValueDescription
profileStringprofileValue for profile scope
openIdStringopenidValue for openid scope
emailStringemailValue for email scope
gameStringhttps://www.huawei.com/auth/gamesValue for game scope
Account
PropertiesTypeDescription
typeStringAccount type of HUAWEI ID
nameStringAccount name of HUAWEI ID
Error Codes
PropertiesTypeValueDescription
SIGN_IN_FAILString101Error code for signIn() method
SIGN_IN_WITH_AUTHORIZATION_CODE_FAILString102Error code for signInWithAuthorizationCode() method
SILENT_SIGN_IN_FAILString103Error code for silentSignIn() method
SIGN_OUT_FAILString104Error code for signOut() method
REVOKE_AUTHORIZATION_FAILString105Error code for revokeAuthorization() method
SMS_VERIFICATION_FAILString106Error code for smsVerification() method
OBTAIN_HASH_CODE_FAILString107Error code for obtainHashCode() method
BUILD_NETWORK_COOKIE_FAILString108Error code for buildNetworkCookie() method
BUILD_NETWORK_URL_FAILString109Error code for buildNetworkUrl() method
ADD_AUTH_SCOPES_FAILString200Error code for addAuthScopes() method
GET_AUTH_RESULT_FAILString201Error code for getAuthResult() method
GET_AUTH_RESULT_WITH_SCOPES_FAILString202Error code for getAuthResultWithScopes() method
CONTAIN_SCOPES_FAILString203Error code for containScopes() method
DELETE_AUTH_INFO_FAILString204Error code for deleteAuthInfo() method
REQUEST_UNION_ID_FAILString205Error code for requestUnionId() method
REQUEST_ACCESS_TOKEN_FAILString206Error code for requestAccessToken() method
TIME_OUTString207Error code for smsVerification() method when verification time is out

HuaweiIdAuthButton

A customizable button to use in authorization pages.

Properties

TypeNameDescription
enumAuthButtonThemeFULL_TITLE represents that button has a logo and a title, NO_TITLE represents that button has only a logo.
enumAuthButtonBackgroundColor options are BLUE, BLACK, GREY, RED and WHITE.
enumAuthButtonRadiusProperty to set button's corner radius. The options are SMALL, MEDIUM and LARGE.
FunctiononPressedAllows button to take a function.
doublewidthSets the width.
doublepaddingSets the vertical padding.
ColortextColorSets the button text color.
doublefontSizeSets the button text size.
FontWeightfontWeightSets the button text weight.

Configuration Description

No

Licensing And Terms

Huawei Account Kit Flutter Plugin uses the Apache 2.0 license.

Libraries

account
auth_huawei_id
auth_param_helper
auth_params
error_codes
hms_account
huawei_id_auth_button
scope