Android 应用快速关联

拥有 OAuth 2.0 实现后,你可以选择配置基于 OAuth 的 App Flip,这可让你的 Android 用户更快地将其在你的身份验证系统中的帐号关联到他们的 Google 帐号。以下部分介绍了如何为 smart home Action 设计和实现 App Flip

Design guidelines

This section describes the design requirements and recommendations for the App Flip account linking consent screen. After Google calls your app, your app displays the consent screen to the user.


  1. You must communicate that the user's account is being linked to Google, not to a specific Google product, such as Google Home or Google Assistant.


We recommend that you do the following:

  1. Display Google's Privacy Policy. Include a link to Google's Privacy Policy on the consent screen.

  2. Data to be shared. Use clear and concise language to tell the user what data of theirs Google requires and why.

  3. Clear call-to-action. State a clear call-to-action on your consent screen, such as "Agree and link." This is because users need to understand what data they're required to share with Google to link their accounts.

  4. Ability to cancel. Provide a way for users to go back or cancel, if they choose not to link.

  5. Ability to unlink. Offer a mechanism for users to unlink, such as a URL to their account settings on your platform. Alternatively, you can include a link to Google Account where users can manage their linked account.

  6. Ability to change user account. Suggest a method for users to switch their account(s). This is especially beneficial if users tend to have multiple accounts.

    • If a user must close the consent screen to switch accounts, send a recoverable error to Google so the user can sign in to the desired account with OAuth linking and the implicit flow.
  7. Include your logo. Display your company logo on the consent screen. Use your style guidelines to place your logo. If you wish to also display Google's logo, see Logos and trademarks.

This figure shows an example consent screen with call-outs to the
            individual requirements and recommendations to be followed when
            you design a user consent screen.
Figure 1: Account linking consent screen design guidelines.

基于 OAuth 的应用快速关联的设置

以下部分描述了基于 OAuth 的应用快速关联的前提条件,以及如何在 Actions 控制台中配置应用快速关联项目。

创建智能家居 Action 并设置 OAuth 2.0 服务器


在 Actions 控制台中配置应用快速关联

以下部分介绍了如何在 Actions 控制台中配置应用快速关联。

  1. 填写 OAuth Client information 下的所有字段。(如果不支持 App Flip,则会使用常规 OAuth 作为后备选项。)
  2. Use your app for account linking (optional) 下方,选中 Enable for Android
  3. 填写以下字段:
    • Application ID。这是你为应用设置的唯一 ID。
    • App signature。Android 应用必须先使用公钥证书进行“签名”,然后才能安装。如需了解如何获取应用签名,请参阅对客户端进行身份验证
    • Authorization intent。在此字段中,输入一个用于指定你的 intent 操作的字符串。
  4. 如果你想选择配置客户端,请添加范围,然后点击 Configure your client (optional) 下的 Add scope
  5. 点击保存

在 Android 应用中实现 App Flip

如需实现应用快速关联功能,你需要修改应用中的用户授权代码,以接受来自 Google 的深层链接。

OAuth-based App Flip linking (App Flip) inserts your Android app into the Google Account Linking flow. A traditional account linking flow requires the user to enter their credentials in the browser. The use of App Flip defers user sign-in to your Android app, which allows you to leverage existing authorizations. If the user is signed in to your app, they don't need to re-enter their credentials to link their account. A minimal amount of code changes are required to implement App Flip on your Android app.

In this document, you learn how to modify your Android app to support App Flip.

Try the sample

The App Flip linking sample app demonstrates an App Flip-compatible account linking integration on Android. You can use this app to verify how to respond to an incoming App Flip intent from Google mobile apps.

The sample app is preconfigured to integrate with the App Flip Test Tool for Android, which you can use to verify your Android app's integration with App Flip before you configure account linking with Google. This app simulates the intent triggered by Google mobile apps when App Flip is enabled.

How it works

The following steps are required to carry out an App Flip integration:

  1. The Google app checks if your app is installed on the device using its package name.
  2. The Google app uses a package signature check to validate that the installed app is the correct app.
  3. The Google app builds an intent to start a designated activity in your app. This intent includes additional data required for linking. It also checks to see if your app supports App Flip by resolving this intent through the Android framework.
  4. Your app validates that the request is coming from the Google app. To do so, your app checks the package signature and the provided client ID.
  5. Your app requests an authorization code from your OAuth 2.0 server. At the end of this flow, it returns either an authorization code or an error to the Google app.
  6. The Google app retrieves the result and continues with account linking. If an authorization code is provided, the token exchange happens server-to-server, the same way it does in the browser-based OAuth linking flow.

Modify your Android app to support App Flip

To support App Flip, make the following code changes to your Android app:

  1. Add an <intent-filter> to your AndroidManifest.xml file with an action string that matches the value you entered in the App Flip Intent field.

    <activity android:name="AuthActivity">
      <!-- Handle the app flip intent -->
        <action android:name="INTENT_ACTION_FROM_CONSOLE"/>
        <category android:name="android.intent.category.DEFAULT"/>
  2. Validate the calling app's signature.

    private fun verifyFingerprint(
            expectedPackage: String,
            expectedFingerprint: String,
            algorithm: String
    ): Boolean {
        callingActivity?.packageName?.let {
            if (expectedPackage == it) {
                val packageInfo =
                    packageManager.getPackageInfo(it, PackageManager.GET_SIGNATURES)
                val signatures = packageInfo.signatures
                val input = ByteArrayInputStream(signatures[0].toByteArray())
                val certificateFactory = CertificateFactory.getInstance("X509")
                val certificate =
                    certificateFactory.generateCertificate(input) as X509Certificate
                val md = MessageDigest.getInstance(algorithm)
                val publicKey = md.digest(certificate.encoded)
                val fingerprint = publicKey.joinToString(":") { "%02X".format(it) }
                return (expectedFingerprint == fingerprint)
        return false
  3. Extract the client ID from the intent parameters and verify that the client ID matches the expected value.

    private const val EXPECTED_CLIENT = "<client-id-from-actions-console>"
    private const val EXPECTED_PACKAGE = "<google-app-package-name>"
    private const val EXPECTED_FINGERPRINT = "<google-app-signature>"
    private const val ALGORITHM = "SHA-256"
    override fun onCreate(savedInstanceState: Bundle?) {
        val clientId = intent.getStringExtra("CLIENT_ID")
        if (clientId == EXPECTED_CLIENT &&
            // ...authorize the user...
  4. Upon successful authorization, return the resulting authorization code back to Google.

    // Successful result
    val data = Intent().apply {
        putExtra("AUTHORIZATION_CODE", authCode)
    setResult(Activity.RESULT_OK, data)
  5. If an error occurred, return an error result instead.

    // Error result
    val error = Intent().apply {
        putExtra("ERROR_TYPE", 1)
        putExtra("ERROR_CODE", 1)
        putExtra("ERROR_DESCRIPTION", "Invalid Request")
    setResult(-2, error)

Content of the launch intent

The Android intent that launches your app includes the following fields:

  • CLIENT_ID (String): Google client_id registered under your app.
  • SCOPE (String[]): A list of scopes requested.
  • REDIRECT_URI (String): The redirect URL.

Content of the response data

The data returned to the Google app is set in your app by calling setResult(). This data includes the following:

  • AUTHORIZATION_CODE (String): The authorization code value.
  • resultCode (int): Communicates the success or failure of the process and takes one of the following values:
    • Activity.RESULT_OK: Indicates success; an authorization code is returned.
    • Activity.RESULT_CANCELLED: Signals that the user has cancelled the process. In this case, the Google app will attempt account linking using your authorization URL.
    • -2: Indicates that an error has occurred. Different types of errors are described below.
  • ERROR_TYPE (int): The type of error, which takes one of the following values:
    • 1: Recoverable error: The Google app will attempt account linking using the authorization URL.
    • 2: Unrecoverable error: The Google app aborts account linking.
    • 3: Invalid or missing request parameters.
  • ERROR_CODE (int): An integer representing the nature of the error. To see what each error code means, refer to the table of error codes.
  • ERROR_DESCRIPTION (String, optional): Human-readable status message describing the error.

A value for the AUTHORIZATION_CODE is expected when resultCode == Activity.RESULT_OK. In all other cases, the value for AUTHORIZATION_CODE needs to be empty. If resultCode == -2, then the ERROR_TYPE value is expected to be populated.

Table of error codes

The table below shows the different error codes and whether each is a recoverable or unrecoverable error:

Error code Meaning Recoverable Unrecoverable

For all error codes, you must return the error result via setResult to ensure the appropriate fallback is triggered.


现在,你已在控制台和应用中创建了一个 Action 并配置了 App Flip,接下来可以在移动设备上测试 App Flip。你可以使用 Google 助理应用或 Google Home 应用测试 App Flip。

如需从 Google 助理应用测试应用快速关联,请按以下步骤操作:

  1. 转到 Actions 控制台并选择你的项目。
  2. 点击顶部导航栏中的 Test
  3. 在 Google 助理应用中触发帐号关联流程:
    1. 打开 Google 助理应用
    2. 点击设置
    3. 在“Google 助理”标签页上,点击家居控制
    4. 点击“添加”(+)
    5. 从提供商列表中选择你的 Action。它在列表中以“[test]”为前缀。从列表中选择 [test] Action 后,就应该会打开你的应用。
    6. 验证你的应用是否已启动并开始测试授权流程。

如需从 Google Home 应用测试应用快速关联,请按以下步骤操作:

  1. 转到 Actions 控制台并选择你的项目。
  2. 点击顶部导航栏中的 Test
  3. 在 Google Home 应用中触发帐号关联流程:
    1. 打开 Google Home 应用
    2. 点击 + 按钮。
    3. 点击设置设备
    4. 点击有已设置好的设备?
    5. 从提供商列表中选择你的智能家居 Action。它在列表中以“[test]”为前缀。从列表中选择 [test] Action 后,就应该会打开你的应用。
    6. 验证你的应用是否已启动并开始测试授权流程。