プラットフォームから直接 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 services] を見つけます。

  2. これらのパッケージのステータスが [Installed] でない場合は、両方を選択して [Install Packages] をクリックします。

アプリを設定する

  1. プロジェクト レベルの build.gradle ファイルで、buildscript セクションと allprojects セクションの両方に Google の Maven リポジトリを含めます。

    buildscript {
        repositories {
            google()
        }
    }
    
    allprojects {
        repositories {
            google()
        }
    }
    
  2. 「Google とのリンク」API の依存関係をモジュールのアプリレベルの Gradle ファイル(通常は app/build.gradle)に追加します。

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

プラットフォーム フローからのリンクにより、サービスから提供されたアクセス トークンが 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. 同意インテントを処理する対応する Activity を作成する

    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 リファレンス ドキュメント