直接在平台上啟動 Google 帳戶連結程序。

您可以直接在行動應用程式中完成帳戶連結程序,讓使用者將您在服務中的帳戶連結至自己的 Google 帳戶。已建立的連結會授權 Google 存取使用者同意分享的資料。

這種做法可讓使用者在熟悉的應用程式環境中連結帳戶,而非透過 Google 助理對話,因此能提升帳戶連結成效。這項功能可整合至使用者入門、設定和其他應用程式介面,為 Google 助理動作創造發掘和參與機會。舉例來說,連結後,您可以提供直接將使用者帶往動作的選項。

使用者可享有的好處包括:

  • 使用者可在應用程式中完成並完成帳戶連結程序。
  • 由於使用者先前已在裝置上和行動應用程式中完成驗證,因此不需要使用登入憑證。

開發人員享有以下好處:

  • 掌控您希望在行動應用程式中宣傳及啟動帳戶連結的位置 (例如使用者設定、插頁式廣告或使用者登入行動應用程式後), ,進而提高參與度和已連結帳戶數量。
  • 轉換率比使用者完成的標準 OAuth 流程更少,因為使用者只需完成幾個步驟就能完成連結程序。
  • 由於您的流程已採用現有的 OAuth2.0 技術,因為這個流程已採用您目前導入的 OAuth2.0 整合功能,因此您不必導入平台 (Android) 即可完成連結。
  • 減少使用者流失率,因為使用者不必重新輸入自己的登入憑證,而且只需完成幾個步驟就能完成程序。 在流程中,流失率可能高達 80%,使用者必須強制回頭輸入登入憑證。

運作方式

您可以透過下列步驟完成平台連結:

  1. 使用者會在行動應用程式中按一下 / 切換連結觸發事件。
  2. 使用者選取要連結的 Google 帳戶。
    1. 使用者在裝置上選取要連結的現有 Google 帳戶,或使用新帳戶登入
  3. 系統會向使用者顯示 Google 代管的同意聲明畫面,使用者必須同意才能繼續操作,或取消以停止連結程序。
  4. 系統會向使用者顯示同意畫面,使用者必須同意才能繼續操作,或取消停止連結程序。
  5. 連結會在使用者帳戶 (在您的服務上) 與其 Google 帳戶之間建立。

圖 1. 透過平台流程連結

需求條件

如要透過平台實作 Link,您需要具備下列項目:

  • Android 應用程式。
  • 擁有、管理及維護支援 OAuth 2.0 授權碼流程的 OAuth 2.0 伺服器。

設定

請先完成帳戶連結註冊程序,再繼續執行下列步驟。

設定開發環境

在開發主機上取得最新的 Google Play 服務:

  1. 開啟 Android SDK Manager
  1. 在「SDK Tools」下方,找出「Google Play 服務」

  2. 如果這些套件的狀態不是「已安裝」,請選取這兩個套件,然後按一下「安裝套件」

設定應用程式

  1. 在專案層級的 build.gradle 檔案中,請同時在 buildscriptallprojects 區段中納入 Google 的 Maven 存放區。

    buildscript {
        repositories {
            google()
        }
    }
    
    allprojects {
        repositories {
            google()
        }
    }
    
  2. 將「Link with Google」API 的依附元件新增至模組的應用程式層級 Gradle 檔案,通常為 app/build.gradle

    dependencies {
      implementation 'com.google.android.gms:play-services-auth:21.4.0'
    }
    

「Link from your Platform」流程會將服務提供的存取權權杖儲存到 Google。您必須先取得同意聲明,才能傳回使用者的權杖。

請按照下列步驟取得使用者的同意,並透過 Google Play 服務 SDK 傳回驗證碼權杖。

  1. 建構可啟動同意活動的 PendingIntent - 同意聲明由 Play 服務 API 啟動。您必須在呼叫 API 時提供 PendingIntent (為了方便說明,以下將稱為 consentPendingIntent)

    Kotlin

    // Build a PendingIntent that can launch the consent activity
    val consentPendingIntent = buildConsentPendingIntent()
    

    Java

    // Build a PendingIntent that can launch your consent activity
    PendingIntent consentPendingIntent =
              buildConsentPendingIntent();
    
  2. 建立對應的活動來處理同意聲明意圖

    Kotlin

      class ConsentActivity : AppCompatActivity
    
      private fun onConsentAccepted() {
          // Obtain a token (for simplicity, we’ll ignore the async nature
          // of the following call)
          val token = getToken()
          val intent = Intent()
                      .putExtra(SaveAccountLinkingTokenRequest.EXTRA_TOKEN,
                                token)
          setResult(Activity.RESULT_OK, intent)
          finish()
      }
    
      private fun onConsentRejectedOrCanceled() {
          setResult(Activity.RESULT_CANCELED)
          finish()
      }
    

    Java

      public class ConsentActivity extends AppCompatActivity {
        ...
        private void onConsentAccepted() {
          // Obtain a token (for simplicity, we’ll ignore the async nature of
          // the following call
          String token = getToken();
          Intent intent = new Intent();
          intent.putExtra(SaveAccountLinkingTokenRequest.EXTRA_TOKEN, token);
          setResult(Activity.RESULT_OK, intent);
          finish();
        }
    
        private void onConsentRejectedOrCanceled() {
          setResult(Activity.RESULT_CANCELED, null);
          finish();
        }
     }
    
    

    我們假設在使用者分別接受或拒絕/取消同意聲明時,會分別呼叫 onConsentAccpeted()onConsentRejectedOrCanceled() 方法。

  3. 建立要求來儲存權杖,並在其他設定參數中傳遞上述步驟 1 中建立的 PendingIntent

    Kotlin

      // Create an ActivityResultLauncher which registers a callback for the
      // Activity result contract
      val activityResultLauncher = registerForActivityResult(
        ActivityResultContracts.StartIntentSenderForResult())
        { result ->
          if (result.resultCode == RESULT_OK) {
            // Successfully finished the flow and saved the token
          } else {
            // Flow failed, for example the user may have canceled the flow
          }
        }
    
      // Build token save request
      val request = SaveAccountLinkingTokenRequest.builder()
        .setTokenType(SaveAccountLinkingTokenRequest.TOKEN_TYPE_AUTH_CODE)
        .setConsentPendingIntent(consentPendingIntent)
        .setServiceId("service-id-of-and-defined-by-developer")
        //Set the scopes that the token is valid for on your platform
        .setScopes(scopes)
        .build()
    
       // Launch consent activity and retrieve token
       Identity.getCredentialSavingClient(this)
         .saveAccountLinkingToken(request)
         .addOnSuccessListener( saveAccountLinkingTokenResult -> {
            if (saveAccountLinkingTokenResult.hasResolution()) {
              val pendingIntent = saveAccountLinkingTokenResult
                                  .getPendingIntent()
              val intentSenderRequest = IntentSenderRequest
                                        .Builder(pendingIntent).build()
              activityResultLauncher.launch(intentSenderRequest)
            } else {
               // This should not happen, let’s log this
               Log.e(TAG, "Failed to save token");
            }
          })
          .addOnFailureListener(e -> Log.e(TAG, Failed to save token, e))
    

    Java

      // Create an ActivityResultLauncher which registers a callback for the
      // Activity result contract
      ActivityResultLauncher<IntentSenderRequest>
          activityResultLauncher =
          registerForActivityResult(new ActivityResultContracts
                                        .StartIntentSenderForResult(),
                                    result -> {
          if (result.getResultCode() == RESULT_OK) {
              // Successfully finished the flow and saved the token
          } else {
              // Flow failed, for example the user may have canceled the flow
          }
      });
    
     // Build token save request
     SaveAccountLinkingTokenRequest request =
        SaveAccountLinkingTokenRequest.builder()
            .setTokenType(
                SaveAccountLinkingTokenRequest.TOKEN_TYPE_AUTH_CODE)
            .setConsentPendingIntent(consentPendingIntent)
            .setServiceId("service-id-of-and-defined-by-developer")
            //Set the scopes that the token is valid for on your platform
            .setScopes(scopes)
            .build();
    
      // Launch consent activity and retrieve token
      Identity.getCredentialSavingClient(this)
          .saveAccountLinkingToken(request)
          .addOnSuccessListener(
              saveAccountLinkingTokenResult -> {
                if (saveAccountLinkingTokenResult.hasResolution()) {
                  // Launch the resolution intent
                  PendingIntent pendingIntent =
                      saveAccountLinkingTokenResult.getPendingIntent();
                  IntentSenderRequest intentSenderRequest =
                      new IntentSenderRequest.Builder(pendingIntent).build();
                  activityResultLauncher.launch(intentSenderRequest);
                } else {
                  // This should not happen, let’s log this
                  Log.e(TAG, "Failed to save token");
                }
              })
          .addOnFailureListener(e -> Log.e(TAG, "Failed to save token", e));
      ```
    

上述步驟會提示使用者同意,並將授權碼傳回給 Google。

最佳做法

  • 應用程式應透過按鈕、切換鈕或類似的視覺元素,向使用者顯示連結狀態。

    圖 1. 連結狀態圖片範例

  • 您應在連結成功後通知使用者,例如顯示快訊、觸發切換狀態變更,或將使用者重新導向至個別連結成功頁面。

  • 您可以考慮在應用程式中提示使用者連結帳戶,最好是根據強烈的信號,說明連結帳戶對使用者有何好處。

  • 連結成功後,請向使用者說明如何使用已連結的帳戶,例如如果您剛連結音樂串流服務,請要求 Google 助理播放音樂。

  • 讓使用者管理已連結的帳戶,包括取消連結的選項。請將他們導向 Google 已連結帳戶管理頁面,也就是 https://myaccount.google.com/accountlinking。

參考資料

Android 驗證 API 參考說明文件