プラットフォームから直接 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. これらのパッケージのステータスが [Installed] でない場合は、両方を選択して [Install Packages] をクリックします。

アプリを設定する

  1. プロジェクト レベルの build.gradle ファイルの buildscript セクションと allprojects セクションの両方に 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.0.0'
    }
    

プラットフォーム フローからリンクすると、サービスによって提供されるアクセス トークンが Google によって保存されます。ユーザーにトークンを返す前に、同意を取得する必要があります。

以下の手順に沿って、ユーザーから同意を得て、Google Play 開発者サービス SDK を介して認証コード トークンを返します。

  1. 同意アクティビティを起動できる PendingIntent を作成します。同意は Play Services 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 Auth API リファレンス ドキュメント