> ## Documentation Index
> Fetch the complete documentation index at: https://docs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Unity で Google Play Games サインインを使用した PlayFab 認証

> Play Console のセットアップとプレイヤー認証をカバーする、PlayFab SDK を使用して Unity プロジェクトに Google Play Games サインインを追加する手順ガイド。

# Unity で Google Play Games サインインを使用した PlayFab 認証のセットアップ

## 前提条件

* Unity ゲーム エンジンを使用して構築されたプロジェクト。
* Google 開発者アカウント。
* PlayFab のゲーム タイトルにアクセスできる PlayFab 開発者アカウント。
* [PlayFab Unity Editor Extensions と Unity SDK](https://docs.xbox.com/ja-jp/services/playfab/sdks/unity3d/quickstart.md)。
* [Google Play Games SDK](https://github.com/playgameservices/play-games-plugin-for-unity)。
* [ログインの基本とベスト プラクティス](https://docs.xbox.com/ja-jp/services/playfab/identity/player-identity/login/login-basics-best-practices.md) に精通していること。

## 開始する

まず、Google Play でいくつかの基本設定を行います。これらの作業を実行するための優れたガイドがいくつかあるため、車輪の再発明はしません。

1. **アプリケーション** を作成します — アプリケーションを作成していない場合は、[Google Play Developer Console](https://play.google.com/apps/publish) で作成する必要があります。

2. ゲームの AAB をビルドし、内部テスト用にアップロードします。[このガイド](https://support.google.com/googleplay/android-developer/answer/9859348?visit_id=638373214207471441-2235763004\&rd=1) に従って、アプリを **Internal testing** トラックにリリースする方法を確認できます。署名プロセスでは、この目的のためにアプリ バンドル用の独自のキーストアを使用する必要があることを考慮してください。キーストアの作成方法については、[このドキュメント](https://docs.unity3d.com/Manual/android-keystore-create.html) を参照してください。

3. このページの手順に従って Google Play Games サービスをセットアップします: [Google Play Games サービスのセットアップ](https://developers.google.com/games/services/console/enabling)。

4. [Google Play Games SDK](https://github.com/playgameservices/play-games-plugin-for-unity) をダウンロードしてインストールします。

5. PlayFab Unity SDK をインストールします (この作業を行う方法がわからない場合は、[Unity3D クイックスタート ガイド](https://docs.xbox.com/ja-jp/services/playfab/sdks/unity3d/quickstart.md) に従ってください)。

## Google Play Games の構成

Google Play Games は、Google Play Games SDK と共にインストールされるツールセットから構成できます。これらのツールにアクセスするには:

* **Window** タブに移動します。
* **Google Play Games** を選択します。
* **Setup** に移動します。
* 次に **Android Setup** にアクセスします。

  ![Google Play Games の構成](https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/google-unity-configuring-google-play-games.png?fit=max\&auto=format\&n=mLCHf0iQv3VidfBe\&q=85\&s=cc4e610420ae613914a545645f437f28)
* Android 構成ウィンドウが表示されます。この画面で Resources Definition と Web App Client ID を入力する必要があります。

  ![Android Resources ペースト 空](https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/google-unity-paste-android-resources-empty.png?fit=max\&auto=format\&n=mLCHf0iQv3VidfBe\&q=85\&s=b13a6bfbd822a8442c007a5163769cf3)

### Resources Definition を取得する

* Android Resources 定義を取得するには、[こちら](https://developer.android.com/games/pgs/unity/unity-start#get-android-resources) の手順に従ってください。

### Web App Client ID を取得する

Google サインインを PlayFab で動作させるには、"Web application" タイプの OAuth Client ID 資格情報を作成することで取得できる Web Client ID を使用する必要があります。

OAuth Client ID を作成する前に、PlayFab **API Endpoint** を知っておく必要があります。これは、次の手順に従って確認できます:

* [Game Manager](https://developer.playfab.com/) にログインします。

* タイトルを選択します。

* タイトル名の横にある **歯車アイコン** をクリックし、**Title Settings** を選択して、タイトル設定に移動します。

  ![PF タイトル設定](https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/google-unity-pf-title-settings.png?fit=max\&auto=format\&n=mLCHf0iQv3VidfBe\&q=85\&s=54512b7841fc8a5d274f0012efca8998)

* **API Features** タブを選択します。

* **API endpoint** フィールドにコピーします。

  ![Google Play Developer Console で API エンドポイントを入力](https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/google-unity-pf-api-endpoint.png?fit=max\&auto=format\&n=mLCHf0iQv3VidfBe\&q=85\&s=80e6521a94996698755cfe971e321438)

タイトルの API Endpoint がわかったので、[こちら](https://developers.google.com/workspace/guides/create-credentials#web-application) に記載されている手順に従って OAuth Client ID 資格情報を作成してください。

* Client ID を作成する際に、API エンドポイントを **Authorized JavaScript origin** として Client ID に追加する必要があります。

  ![認証済み JavaScript オリジン](https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/google-unity-authorized-javascript-origins.png?fit=max\&auto=format\&n=mLCHf0iQv3VidfBe\&q=85\&s=9a2f0d445c948f5386bae1350a4d5074)

* また、独自のアプリケーション リダイレクト URI を **Authorized Redirect URI** として追加してください。この手順を行わないと、**redirect\_uri の不一致** エラーが発生します。

  ![認証済みリダイレクト URI](https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/google-unity-authorized-redirect-uris.png?fit=max\&auto=format\&n=mLCHf0iQv3VidfBe\&q=85\&s=dc66e1b57e200b52f04f5f0ac13f16a2)

Client ID を作成すると、次のような画面が表示され、生成された Client ID 文字列と Client Secret を取得できます。

![OAuth Client ID 作成完了](https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/google-unity-client-id-created.png?fit=max\&auto=format\&n=mLCHf0iQv3VidfBe\&q=85\&s=87ed059a93128acb745348cdfb97e859)

これで、Unity の Android Configuration ウィンドウに戻り、**Client ID** テキストボックスに Client ID を貼り付けることができます:

![Client ID をペースト](https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/google-unity-paste-client-id.png?fit=max\&auto=format\&n=mLCHf0iQv3VidfBe\&q=85\&s=e880647b4bccd88ff590b840b8d1f642)

両方の値が入力されたら、**Setup** を選択できます:

![Android 構成のセットアップ完了](https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/google-unity-setup-android-configuration.png?fit=max\&auto=format\&n=mLCHf0iQv3VidfBe\&q=85\&s=83928abc8c46b9fac2698afc9f069219)

## PlayFab Google サインインのセットアップ

PlayFab には、Google サインインをセットアップするための独自のプロセスがあります。このセットアップは、PlayFab 開発者アカウントで行うことができます。持っていない場合は、[無料の開発者アカウントにサインアップ](https://developer.playfab.com) できます。

PlayFab で Google をセットアップするには:

* ダッシュボードの **Add-ons** セクションに移動します。
* **Google Add-On** に移動します。
* そこから **Install Google** ボタンを選択します。

このフォームに入力する必要があるいくつかの項目があります:

* **Google App Package ID** — このフィールドは Android パッケージの名前です。たとえば、サンプルでは `com.playfab.gpgexample` を使用しています。
* **Google App License Key** — このフィールドは必須ですが、Google サインインとは関係ありません。このフィールドは、**Receipt Validation API** を使用したレシート検証の設定に使用します。

> [!NOTE]
>
> この License Key は [Google Developer Console](https://play.google.com/apps/publish) から取得できます。アプリに移動し、**Monetize** セクションの下にある **Monetization Setup** というセクションを選択します。その中で、**Licensing** テキストボックスが見えるまで下にスクロールし、Base64 でエンコードされた RSA 公開キーをこのフィールドにコピーして貼り付けます。

* **Google OAuth Client ID** — Unity の **Google Play Games SDK** セットアップで使用した Web Client ID。
* **Google OAuth Client Secret** — OAuth Client ID を作成したときに取得したシークレット キー。
* **Service Account Key** — このフィールドはサービス アカウント キーの JSON 文字列を想定しています。[こちら](https://developers.google.com/workspace/guides/create-credentials#create_a_service_account) の手順に従ってサービス アカウント キーを作成する必要があります。次に、[こちら](https://developers.google.com/workspace/guides/create-credentials#create_credentials_for_a_service_account) の手順に従ってサービス アカウントの資格情報を作成します。資格情報 JSON ファイルを取得したら、JSON の内容全体をこのフィールドに貼り付けます。

アドオン構成は、保存前に次のようになります:

![PlayFab の Google アドオン セットアップ](https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/google-unity-google-addon-config.png?fit=max\&auto=format\&n=mLCHf0iQv3VidfBe\&q=85\&s=481bc6a5ddebf25c3194630e2444c753)

> [!TIP]
>
> **Save Settings** を忘れずに実行してください!

## Unity ゲームに Google サインインを追加する

Google サインインを動作させるためのコードは非常にシンプルです。初期化コードは、Start (またはアプリを初期化または起動する任意の場所) に配置します。

このコードは、ゲームやアプリのアーキテクチャとフレームワークに応じて、さまざまな場所に配置できます。いずれの場合も、次のコードは Google Play Games を初期化します。

> [!NOTE]
>
> **using** ステートメントを忘れないようにして、Google Play Games SDK を使用できるようにしてください。

```csharp theme={null}
using GooglePlayGames;
using GooglePlayGames.BasicApi;
using PlayFab;
using PlayFab.ClientModels;
using UnityEngine;

public class PFGoogleSignInUnity : MonoBehaviour
{
    void Start()
    {
        PlayGamesPlatform.Instance.Authenticate(ProcessAuthentication);
    }

    internal void ProcessAuthentication(SignInStatus status)
    {
        if (status == SignInStatus.Success)
        {
            PlayGamesPlatform.Instance.RequestServerSideAccess(false, ProcessServerAuthCode);
        }
    }

    private void ProcessServerAuthCode(string serverAuthCode)
    {
        Debug.Log("Server Auth Code: " + serverAuthCode);

        var request = new LoginWithGooglePlayGamesServicesRequest
        {
            ServerAuthCode = serverAuthCode,
            CreateAccount = true,
            TitleId = PlayFabSettings.TitleId
        };

        PlayFabClientAPI.LoginWithGooglePlayGamesServices(request, OnLoginWithGooglePlayGamesServicesSuccess, OnLoginWithGooglePlayGamesServicesFailure);
    }

    private void OnLoginWithGooglePlayGamesServicesSuccess(LoginResult result)
    {
        Debug.Log("PF Login Success LoginWithGooglePlayGamesServices");
    }

    private void OnLoginWithGooglePlayGamesServicesFailure(PlayFabError error)
    {
        Debug.Log("PF Login Failure LoginWithGooglePlayGamesServices: " + error.GenerateErrorReport());
    }
}
```

何が起こっているかを正確に説明します:

* まず、`PlayGamesPlatform.Instance.Authenticate` でログインします — この関数は結果を処理するコールバック関数をパラメーターとして受け取り、受け取った *SignInStatus* に基づいてユーザーが正常にログインできたかどうかを知らせます。
* テスト アカウントをアプリに追加しないと、コールバック *SignInStatus* の結果は、テスト中は常に *Canceled* になります。この問題が発生した場合は、[このガイド](https://developers.google.com/games/services/console/testpub) の手順に従って、Google アプリケーションにテスターを追加してください。

> [!NOTE]
>
> `PlayGamesPlatform.Instance.Authenticate` の呼び出しにより、**Google Play Games** サインイン ダイアログがポップアップ表示されます。

* 次に、ログインに成功した場合、`PlayGamesPlatform.Instance.RequestServerSideAccess` を呼び出します。これは別のコールバックを受け取り、ServerAuthCode 文字列を返します。

* 次に、`PlayFabClientAPI.LoginWithGooglePlayGamesServices` を呼び出し、Google から取得した `AuthCode` を `ServerAuthCode` パラメーターとして渡します。

* 最後に、`LoginWithGooglePlayGamesServices` の呼び出しの結果に応じて、`OnLoginWithGooglePlayGamesServicesSuccess` または `OnLoginWithGooglePlayGamesServicesFailure` が呼び出されます。これらのコールバックは、PlayFab へのログイン操作結果を処理します。この時点で、ダッシュボードにログイン成功が表示されるはずです!

このガイドがお役に立てば幸いです。ただし、質問がある場合は、遠慮なく [コミュニティ フォーラム](https://community.playfab.com/) にお問い合わせください。


## Related topics

- [プラットフォーム固有の認証](https://docs.xbox.com/ja-jp/services/playfab/identity/player-identity/platform-specific-authentication/index.md)
- [Google マーケットプレイスでの引き換え](https://docs.xbox.com/ja-jp/services/playfab/economy-monetization/economy-v2/marketplace/marketplace-redemption/google.md)
- [Unity で Google サインインから Google Play Games サインインへの移行のフォールバック](https://docs.xbox.com/ja-jp/services/playfab/identity/player-identity/platform-specific-authentication/google-play-games-sign-in-migration-fallback.md)
- [Unity で Google サインインから Google Play Games サインインに移行する手順](https://docs.xbox.com/ja-jp/services/playfab/identity/player-identity/platform-specific-authentication/google-play-games-sign-in-migration-details.md)
- [Unity で Google サインインから Google Play Games サインインへ移行する](https://docs.xbox.com/ja-jp/services/playfab/identity/player-identity/platform-specific-authentication/google-play-games-sign-in-migration.md)
