什么是AppGallery Connect认证服务?

AppGallery Connect认证服务提供了云侧服务和SDK,可以帮助开发者为应用快速构建安全可靠的用户认证系统,以便应用可以对用户进行身份认证。AppGallery Connect认证服务支持多种认证方式,并与其他Serverless服务紧密集成,开发者可以通过简单的规则定义来保护用户的数据安全。
通过集成AppGallery Connect认证服务提供的Auth Service SDK,开发者可以对接包括华为帐号、微信、Facebook、Twitter在内的多种第三方用户认证系统,并将认证凭据上报给AppGallery Connect认证服务云侧。在下次登录时,应用便可直接通过AppGallery Connect认证服务获取用户的个人基本信息和其他Serverless服务中受安全规则保护的数据。
AppGallery Connect认证服务可以极大减少开发者在用户认证系统构建和运维上的投入和成本。

您将建立什么

在本次Codelab中,您将建立一个能够具备使用AppGallery Connect认证服务匿名帐号与认证华为帐号的Android应用程序,您的应用程序将包含:

您将会学到什么

开发环境及技能要求

运行终端要求

必须为Android 4.2及以上版本。

集成AppGallery Connect认证服务,需要完成以下准备工作

具体操作,请按照《AppGallery Connect集成准备》中详细说明来完成。
  1. AppGallery Connect页面点击"我的项目",在项目的应用列表中选择您需要开通认证服务的应用。
  2. 在导航栏选择"构建 > 认证服务",点击"立即开通"开通服务。
  3. 点击需要启动的认证方式所在行的"启用"。本次Codelab选择启用"华为帐号"与"匿名帐号"。
    启用华为帐号时需要输入Client ID和Client Secret,可以在项目设置中找到。

针对Android Studio开发环境,华为提供了maven仓集成方式的SDK包,开发前需集成SDK到您的Android Studio项目中。

  1. AppGallery Connect页面点击"我的项目",在项目下的应用列表中选择您需要开通认证服务的应用。
  2. 在项目设置的"常规"页签下,点击"应用"栏下的"agconnect-services.json"下载配置文件。
  3. 将"agconnect-services.json"文件拷贝到应用级根目录下。
  4. 打开Android Studio应用级build.gradle文件,添加如下代码集成Auth SDK。
    //配置如下地址 apply plugin: 'com.huawei.agconnect' dependencies { //配置如下地址 implementation 'com.huawei.agconnect:agconnect-auth:1.6.5.300' implementation "com.huawei.agconnect:agconnect-auth-huawei:1.6.5.300" }
  5. 点击界面上的"Sync Now"链接同步已完成的配置。

本次Codelab中您可以在Android Studio工程中创建一个布局页面,参照下图进行UI设计,具备简单的匿名帐号与华为帐号绑定入口即可。

布局代码如下:

<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Welcome to the Codelab" android:textSize="24dp" android:textAlignment="center" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.2" /> <Button android:id="@+id/anonymous_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Anonymous Login" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.35" /> <TextView android:id="@+id/anonymous_result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="24dp" android:textAlignment="center" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.5" /> <Button android:id="@+id/huaweiid_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="HuaweiID Login" android:textAllCaps="false" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.65" /> <TextView android:id="@+id/result_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="24dp" android:textAlignment="center" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.8" /> </androidx.constraintlayout.widget.ConstraintLayout>
  1. 点击ANONYMOUS LOGIN,在登录成功后打印当前用户的uid。
    Button anonymousBtn = findViewById(R.id.anonymous_button); anonymousBtn.setOnClickListener(view -> AGConnectAuth.getInstance().signInAnonymously() .addOnSuccessListener(signInResult -> { AGConnectUser user = signInResult.getUser(); String uid = user.getUid(); anonymousResult.setText(uid); Log.i(TAG, "UidValue: " + uid); }).addOnFailureListener(e -> { anonymousResult.setText("Anonymous SignIn failed"); Log.e(TAG, "UidValue: " + e.getMessage()); }));
  2. 点击HuaweiID Login,将华为帐号绑定到刚刚登录的匿名帐号,在绑定成功后打印当前用户的uid,对比与刚刚匿名帐号的uid是否一致。
    Button huaweiidButton = findViewById(R.id.huaweiid_button); huaweiidButton.setOnClickListener(view -> AGConnectAuth.getInstance().getCurrentUser().link(MainActivity.this, AGConnectAuthCredential.HMS_Provider) .addOnSuccessListener(signInResult -> { String hwUid = signInResult.getUser().getUid(); result.setText(hwUid); Log.i(TAG, "UidValue: " + hwUid); }).addOnFailureListener(e -> { result.setText("HWID SignIn failed"); Log.e(TAG, "UidValue: " + e.getMessage()); }));
  1. 运行Android Studio工程生成APK包,打包时必须有应用签名,并将SHA256签名配置到AppGallery Connect项目设置中。
  2. 在测试手机中安装APK包。
  3. 点击ANONYMOUS LOGIN,观察界面是否可以打印出当前用户的uid。
  4. 点击HuaweiID Login按钮,观察是否会展示华为帐号绑定后的uid,并对比是否与匿名帐号登录的一致。

祝贺您,您已经成功地构建了您的第一个集成AppGallery Connect认证服务的应用程序,并学到了如何使用AppGallery Connect认证服务构建用户认证系统。

AppGallery Connect认证服务的相关API介绍请参见API索引
本Codelab中所用demo源码下载链接:源码下载

Code copied