> ## 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.

# 從 Google 登入移轉至 Play Games 登入的後援方法

> 針對升級 Play Games Plugin for Unity 外掛程式後仍未使用 Google Play Games 身分識別之玩家的後援方法。

# 在 Play Games Plugin for Unity v0.11.x 上使用 LoginWithGoogleAccount 的修正方法

本文說明當您升級至某個版本的 Play Games Plugin for Unity 後，使用 **LoginWithGoogleAccount** API 時收到錯誤的因應措施。

當您將 [Play Games Plugin for Unity](https://github.com/playgameservices/play-games-plugin-for-unity) 升級至 v0.11.x 或更新版本後，如果現有玩家尚未[移轉為使用 LoginWithGooglePlayGamesServices](/zh-TW/services/playfab/identity/player-identity/platform-specific-authentication/google-play-games-sign-in-migration-details#migration-steps)，就會發生此錯誤。原因是 Google 在 0.11.x 或更新版本的外掛程式中移除了新增額外範圍的功能。

由於必須透過瀏覽器登入可能會影響玩家體驗，因此應將此因應措施視為後援替代方案。

## 開始之前

依照下列高階步驟，修正使用最新版本外掛程式時的驗證問題。

1. 手動呼叫 Google OAuth2 API
2. 在瀏覽器索引標籤中執行玩家驗證
3. 使用 Android 深層連結將重新導向驗證碼傳回遊戲。

## 建議

在您仍使用 0.10.x 版外掛程式時，[將使用者移轉為使用 LoginWithGooglePlayGamesServices](/zh-TW/services/playfab/identity/player-identity/platform-specific-authentication/google-play-games-sign-in-migration-details#migration-steps) API。升級至 0.11.x 或更新版本的外掛程式後，將下列步驟作為其餘玩家的後援方法。

## 修正步驟

1. 使用深層連結將 Android 意圖新增至您的遊戲。(步驟改編自 [Unity - Manual: Deep linking on Android (unity3d.com)](https://docs.unity3d.com/2021.2/Documentation/Manual/deep-linking-android.html))

   a. 在 Project 視窗中，前往 **Assets > Plugins > Android**。

   b. 建立新檔案並命名為 **AndroidManifest.xml**。當您建置應用程式時，Unity 會自動處理此檔案。

   c. 將下列資訊清單範例複製到新檔案中，以您應用程式的 URI 更新 data 元素並儲存。如需如何建置正確 data 標籤的詳細資訊，請參閱[這篇文章](https://developer.android.com/guide/topics/manifest/data-element)。

   ```xml theme={null}
   <?xml version="1.0" encoding="utf-8"?>
   <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
       <application>
       <activity android:name="com.unity3d.player.UnityPlayerActivity" android:theme="@style/UnityThemeSelector">
           <intent-filter>
           <action android:name="android.intent.action.MAIN" />
           <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>

           <intent-filter android:autoVerify="true">
           <action android:name="android.intent.action.VIEW" />

           <category android:name="android.intent.category.DEFAULT" />
           <category android:name="android.intent.category.BROWSABLE" />

           <data
               android:scheme="https"
               android:host="mytestapp.com"
               android:path="/path1/subpath" />
           </intent-filter>
           
       </activity>
       </application>
   </manifest>
   ```

   d. 確認 intent-filter 標籤上有 **autoVerify** 屬性。

2. 為了讓深層連結能夠運作，從 Android 12 開始，Google 現在要求開發人員在已知位置裝載 assetlinks.json 檔案，才能成功重新導向至您的應用程式。

   例如，如果您的重新導向 URI 是 [https://mytestapp.com/path1/subpath，您應該在](https://mytestapp.com/path1/subpath，您應該在) [https://mytestapp.com/.well-known/assetlinks.json](https://mytestapp.com/.well-known/assetlinks.json) 裝載有效的 assetlinks.json 檔案，否則深層連結將無法運作，您也不會被重新導向回應用程式以繼續操作。
   如需如何建立 assetlinks.json 檔案的詳細資訊，請參閱 [Verify Android App Links](https://developer.android.com/training/app-links/verify-android-applinks)。

3. 將深層連結處理新增至您的遊戲。(步驟改編自 [Unity - Manual: Deep linking (unity3d.com)](https://docs.unity3d.com/2021.2/Documentation/Manual/deep-linking.html#using-deep-links))。

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

   public class ProcessDeepLinkMngr : MonoBehaviour
   {
       public static ProcessDeepLinkMngr Instance { get; private set; }

       private void Awake()
       {
           if (Instance == null)
           {
               Instance = this;
               Application.deepLinkActivated += onDeepLinkActivated;
               if (!string.IsNullOrEmpty(Application.absoluteURL))
               {
                   // Cold start and Application.absoluteURL not null so process Deep Link.
                   onDeepLinkActivated(Application.absoluteURL);
               }

               DontDestroyOnLoad(gameObject);
           }
           else
           {
               Destroy(gameObject);
           }
       }

       private void onDeepLinkActivated(string url)
       {
           Debug.Log("Got Deeplink Activated: " + url);

           // Decode the URL to extract auth code. 
           string queryParams = url.Split("?"[0])[1];
           string[] keyValuePairs = queryParams.Split("&");
           string authCode = string.Empty;

           foreach (string s in keyValuePairs)
           {
               if (s.StartsWith("code"))
               {
                   authCode = s.Split("=")[1];
                   break;
               }
           }

           if (!string.IsNullOrEmpty(authCode))
           {
               // Call the LoginWithGoogleAccount using the auth code
               Debug.Log("Authenticating to PlayFab using LoginWithGoogleAccount...");

               // Make sure to unescape string
               authCode = Uri.UnescapeDataString(authCode);

               var request = new LoginWithGoogleAccountRequest
               {
                   ServerAuthCode = authCode,
                   CreateAccount = true,
                   TitleId = PlayFabSettings.TitleId
               };

               PlayFabClientAPI.LoginWithGoogleAccount(request,
                   (LoginResult result) => {
                       Debug.Log("PlayFab LoginWithGoogleAccount Success.");
                   },
                   (PlayFabError error) => {
                       Debug.Log("PlayFab LoginWithGoogleAccount Failure: " + error.GenerateErrorReport());
                   }
               );
           }
           else
           {
               Debug.Log("Error when getting Auth Code.");
           }
       }
   }
   ```

4. 新增一些程式碼，以啟動瀏覽器前往 Google OAuth2 API，並讓玩家透過瀏覽器登入。如果玩家已在瀏覽器上登入，轉換過程將會很順暢，否則他們需要輸入認證。

   ```csharp theme={null}
   private string authorizationEndpoint = "https://accounts.google.com/o/oauth2/v2/auth";
   private string redirectURI = "[APPLICATION-REDIRECT-URI]";
   private object clientID = "[WEB-APPLICATION-CLIENT-ID]";
   private string scopes = "profile";

   private void LaunchBrowserAuth()
   {
       string authorizationRequest = string.Format("{0}?response_type=code&scope={1}&redirect_uri={2}&client_id={3}",
               authorizationEndpoint,
               scopes,
               Uri.EscapeDataString(redirectURI),
               clientID);

       Application.OpenURL(authorizationRequest);
   }
   ```

   重新導向 URI 必須與我們在步驟 1 中新增的 Android 意圖篩選器相符。因此，當使用者完成驗證時，瀏覽器會瀏覽至該重新導向 URI，並應詢問使用者是否要瀏覽回應用程式。

5. 現在，您應該已準備好進行測試。當您觸發驗證時，應該會開啟瀏覽器，使用者必須依序登入。除非刪除瀏覽器資料，否則此步驟應該只需執行一次。

   <img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/LWGA-fix-1.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=f6b510051e5e1f3456012cb7ed882174" alt="使用 LoginWithGoogleAccount 步驟 1" width="413" height="881" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/LWGA-fix-1.png" />

6. 使用者完成驗證後，瀏覽器會詢問應以哪個應用程式開啟重新導向 URL。

   <img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/LWGA-fix-2.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=97d1b122fb3b35e0fa1cd97d2422200a" alt="使用 LoginWithGoogleAccount 步驟 2" width="412" height="879" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/LWGA-fix-2.png" />

7. 此動作會瀏覽回我們的應用程式，我們可以從重新導向 URL 的查詢參數取得代碼，然後繼續呼叫 LoginWithGoogleAccount。

   <img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/LWGA-fix-3.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=5286049daf40d5f57ae37bb3dce570a8" alt="使用 LoginWithGoogleAccount 步驟 3" width="415" height="879" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/LWGA-fix-3.png" />

8. 最後，我們建議將此方法作為無法使用[移轉步驟](/zh-TW/services/playfab/identity/player-identity/platform-specific-authentication/google-play-games-sign-in-migration-details#migration-steps)移轉之使用者的後援替代方案。

***請記住，您應該追蹤哪些使用者已移轉為使用 LoginWithGooglePlayGamesServices，並優先使用外掛程式提供的自動登入機制，僅將此方法作為尚未移轉之使用者的因應措施。***


## Related topics

- [在 Unity 中從 Google 登入移轉至 Google Play Games](/zh-TW/services/playfab/identity/player-identity/platform-specific-authentication/google-play-games-sign-in-migration-details.md)
- [將 Unity 遊戲移轉至 Google Play Games 登入](/zh-TW/services/playfab/identity/player-identity/platform-specific-authentication/google-play-games-sign-in-migration.md)
- [登入 Game Manager](/zh-TW/services/playfab/live-service-management/gamemanager/game-manager-login.md)
- [從 PlayFab 獨立 SDK v1 移轉至 Unified SDK v2](/zh-TW/services/playfab/sdks/unified-sdk/migrating-from-v1.md)
- [登入基本概念和最佳做法](/zh-TW/services/playfab/identity/player-identity/login/login-basics-best-practices.md)
