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

# 在 Unity 中從 Google 登入移轉至 Google Play Games 登入的步驟

> 將 Unity 玩家從 Google 帳戶登入切換至 LoginWithGooglePlayGamesServices 的詳細移轉步驟，包括連結和驗證。

# LoginWithGooglePlayGamesServices API 移轉程序

由於 Google 鼓勵開發人員移轉至這種驗證方式，因此移轉的內容是切換至 LoginWithGooglePlayGamesServices。移轉玩家之後，將不再依賴 LoginWithGoogleAccount API，開發人員應可升級至最新的外掛程式版本。

如需詳細資訊，請參閱 [Google 官方文件](https://developers.google.com/games/services/android/signin#migrate_to_play_games_services_sign_in_v2)。

## 高階步驟

1. 選擇正確版本的 "Play Games Plugin for Unity"。
2. 初始化 Play Games 平台並驗證使用者。
3. 使用 LoginWithGoogleAccount 登入 PlayFab。
4. 將您的 Google Play Games 設定檔連結至 PlayFab 玩家帳戶。
5. 確認使用者可以使用 LoginWithGooglePlayGamesServices API 登入。
6. (選擇性) 將您的 Google 帳戶設定檔與 PlayFab 玩家帳戶取消連結。

## 移轉步驟

### 選擇正確版本的 "Play Games Plugin for Unity"

由於 Google 不再允許要求額外的範圍，因此最新版本的外掛程式無法用於移轉，PlayFab 的 LoginWithGoogleAccount API 也不再運作。

這些移轉步驟是使用 "Play Games Plugin for Unity" 0.10.14 版進行測試和記錄的，您可以在此處找到該版本：[playgameservices/play-games-plugin-for-unity at v10.14 (github.com)](https://github.com/playgameservices/play-games-plugin-for-unity/tree/v10.14)

如需外掛程式安裝和設定步驟，請參閱外掛程式的 [README](https://github.com/playgameservices/play-games-plugin-for-unity/blob/v10.14/README.md)

### 初始化 Play Games 平台並驗證玩家

請參閱 [README 中的 Configuration & Initialization Play Games Services 一節](https://github.com/playgameservices/play-games-plugin-for-unity/blob/v10.14/README.md#configuration--initialization-play-game-services)，並在 PlayGamesClientConfiguration 中要求 "profile" 範圍。

```csharp theme={null}
 PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()
     .AddOauthScope("profile")
     .RequestServerAuthCode(false)
     .Build();
 
 PlayGamesPlatform.InitializeInstance(config);
 // recommended for debugging:
 PlayGamesPlatform.DebugLogEnabled = true;
 // Activate the Google Play Games platform
 PlayGamesPlatform.Activate();
 
 // authenticate user:
 PlayGamesPlatform.Instance.Authenticate(SignInInteractivity.CanPromptOnce, (SignInStatus result) => {
     if (result == SignInStatus.Success)
     {
         Debug.Log("Authentication Succeeded.");
     }
     else
     {
         Debug.Log("Authentication Failed. SignInStatus: " + result);
     }
 });
```

### 使用 LoginWithGoogleAccount 登入 PlayFab

使用 [LoginWithGoogleAccount](https://learn.microsoft.com/en-us/rest/api/playfab/client/authentication/login-with-google-account?view=playfab-rest) 驗證 API 登入現有玩家的帳戶。若要成功登入，您需要提供伺服器驗證權杖，可透過 GetServerAuthCode 方法要求。

```csharp theme={null}
 public void PlayFabLoginWithGoogleAccount()
 {
     var serverAuthCode = PlayGamesPlatform.Instance.GetServerAuthCode();

     var request = new LoginWithGoogleAccountRequest
     {
         ServerAuthCode = serverAuthCode,
         TitleId = PlayFabSettings.TitleId
     };
 
     PlayFabClientAPI.LoginWithGoogleAccount(request, OnLoginWithGoogleAccountSuccess, OnLoginWithGoogleAccountFailure);
 }

 private void OnLoginWithGoogleAccountSuccess(LoginResult result)
 {
     Debug.Log("PlayFab LoginWithGoogleAccount Success.");
 }

 private void OnLoginWithGoogleAccountFailure(PlayFabError error)
 {
     Debug.Log("PlayFab LoginWithGoogleAccount Failure: " + error.GenerateErrorReport());
 }
```

在進行任何移轉之前，連結至 Google 帳戶的使用者在 [Game Manager](https://developer.playfab.com) 上會顯示如下：

![移轉至 LoginWithGooglePlayGamesServices 步驟 1](https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/LWGPGS-migration-procedure-1.png?fit=max\&auto=format\&n=mLCHf0iQv3VidfBe\&q=85\&s=6831f4c076155602f1661a9e7822656a)

### 將玩家的 Google Play Games 設定檔連結至 PlayFab 玩家帳戶

在此步驟中，您會將玩家的 Google Play Games 帳戶連結至先前僅連結至 Google 帳戶的現有玩家 PlayFab 帳戶。

```csharp theme={null}
public void LinkGooglePlayGamesAccount()
{
   PlayGamesPlatform.Instance.GetAnotherServerAuthCode(true, (serverAuthCode) => {

       var linkRequest = new LinkGooglePlayGamesServicesAccountRequest
       {
           ServerAuthCode = serverAuthCode
       };

       PlayFabClientAPI.LinkGooglePlayGamesServicesAccount(linkRequest, OnLinkGooglePlayGamesServicesAccountSuccess, OnLinkGooglePlayGamesServicesAccountFailure);
   });
}

private void OnLinkGooglePlayGamesServicesAccountSuccess(LinkGooglePlayGamesServicesAccountResult result)
{
   Debug.Log("PlayFab LinkGooglePlayGamesServicesAccount Success");
}

private void OnLinkGooglePlayGamesServicesAccountFailure(PlayFabError error)
{
   Debug.Log("PlayFab LinkGooglePlayGamesServicesAccount Failure: " + error.GenerateErrorReport());
}
```

完成此步驟後，玩家在 PlayFab 端應已關聯兩個帳戶設定檔，從現在起應可開始使用 [LoginWithGooglePlayGamesServices](https://learn.microsoft.com/en-us/rest/api/playfab/client/authentication/login-with-google-play-games-services?view=playfab-rest) 驗證 API。

如果您瀏覽至 [Game Manager](https://developer.playfab.com)，會看到兩個帳戶都已與玩家關聯，如下所示：

![移轉至 LoginWithGooglePlayGamesServices 步驟 2](https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/LWGPGS-migration-procedure-2.png?fit=max\&auto=format\&n=mLCHf0iQv3VidfBe\&q=85\&s=c0a4ea9b73a4d5a2dcdaa326dfce3e63)

### 確認玩家可以使用 LoginWithGooglePlayGamesServices API 登入

```csharp theme={null}

 public void PFLoginWithGooglePlayGames()
 {
     PlayGamesPlatform.Instance.GetAnotherServerAuthCode(true, (serverAuthCode) => {

         var request = new LoginWithGooglePlayGamesServicesRequest
         {
             ServerAuthCode = serverAuthCode,
             CreateAccount = false,
             TitleId = PlayFabSettings.TitleId
         };
 
         PlayFabClientAPI.LoginWithGooglePlayGamesServices(request, OnLoginWithGooglePlayGamesServicesSuccess, OnLoginWithGooglePlayGamesServicesFailure);
     });
 }

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

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

```

### (選擇性) 將您的 Google 帳戶設定檔與 PlayFab 玩家帳戶取消連結

此步驟為選擇性。不過，如果您已確認 Google Play Games Services 設定檔已連結至玩家帳戶，且可以成功登入 PlayFab，則您可能會想要使用 [UnlinkGoogleAccount](https://learn.microsoft.com/en-us/rest/api/playfab/client/account-management/unlink-google-account?view=playfab-rest) API，將先前連結至玩家設定檔的所有 Google 帳戶設定檔取消連結，並改為只使用 [LoginWithGooglePlayGamesServices](https://learn.microsoft.com/en-us/rest/api/playfab/client/authentication/login-with-google-play-games-services?view=playfab-rest) API。

```csharp theme={null}
 public void UnlinkGoogleAccountFromPlayer()
 {
     PlayFabClientAPI.UnlinkGoogleAccount(new UnlinkGoogleAccountRequest(), OnUnlinkGoogleAccountSuccess, OnUnlinkGoogleAccountFailure);
 }
 
 private void OnUnlinkGoogleAccountSuccess(UnlinkGoogleAccountResult result)
 {
     Debug.Log("PlayFab UnlinkGoogleAccount Success.");
 }
 
 private void OnUnlinkGoogleAccountFailure(PlayFabError error)
 {
     Debug.Log("PlayFab UnlinkGoogleAccount Failure: " + error.GenerateErrorReport());
 }
```

取消連結後，您在 [Game Manager](https://developer.playfab.com) 上應該只會看到該玩家的 Google Play Games 身分識別：

![移轉至 LoginWithGooglePlayGamesServices 步驟 3](https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/LWGPGS-migration-procedure-3.png?fit=max\&auto=format\&n=mLCHf0iQv3VidfBe\&q=85\&s=1e0da657f9f650df7ec1009d4bfdf2dd)


## Related topics

- [在 Unity 中從 Google 登入移轉至 Google Play Games 登入的後援](/zh-TW/services/playfab/identity/player-identity/platform-specific-authentication/google-play-games-sign-in-migration-fallback.md)
- [在 Unity 中從 Google 登入移轉至 Google Play Games 登入](/zh-TW/services/playfab/identity/player-identity/platform-specific-authentication/google-play-games-sign-in-migration.md)
- [在 Unity 中使用 Google Play Games 登入進行 PlayFab 驗證](/zh-TW/services/playfab/identity/player-identity/platform-specific-authentication/google-sign-in-unity.md)
- [登入 Game Manager](/zh-TW/services/playfab/live-service-management/gamemanager/game-manager-login.md)
- [PFAuthenticationLoginWithGooglePlayGamesServicesRequest](/zh-TW/services/playfab/api-references/c/pfauthenticationtypes/structs/pfauthenticationloginwithgoogleplaygamesservicesrequest.md)
