You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity集成Google Play Games登录失败问题求助

配置Google Play Games服务时持续报错:Failed to retrieve Google play games authorization code

报错信息

在移动端和Unity编辑器中均触发以下异常:

Exception: Failed
GPmanager.Start () (at Assets/Scripts/menu/GPmanager.cs:25)
System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) (at <bfbc89a04b6b4dd49c3614d03fc31ce8>:0)
UnityEngine.UnitySynchronizationContext+WorkRequest.Invoke () (at <f7237cf7abef49bfbb552d7eb076e422>:0)
UnityEngine.UnitySynchronizationContext.Exec () (at <f7237cf7abef49bfbb552d7eb076e422>:0)
UnityEngine.UnitySynchronizationContext.ExecuteTasks () (at <f7237cf7abef49bfbb552d7eb076e422>:0)

同时GPManager脚本输出错误:Failed to retrieve Google play games authorization code

GPManager脚本代码

public string Token;
public string Error;

void Awake()
{
    PlayGamesPlatform.Activate();
}

async void Start()
{
    await UnityServices.InitializeAsync();
    await LoginGooglePlayGames();
    await SignInWithGooglePlayGamesAsync(Token);
}
//Fetch the Token / Auth code
public Task LoginGooglePlayGames()
{
    var tcs = new TaskCompletionSource<object>();
    PlayGamesPlatform.Instance.Authenticate((success) =>
    {
        if (success == SignInStatus.Success)
        {
            Debug.Log("Login with Google Play games successful.");
            PlayGamesPlatform.Instance.RequestServerSideAccess(true, code =>
            {
                Debug.Log("Authorization code: " + code);
                Token = code;
                // This token serves as an example to be used for SignInWithGooglePlayGames
                tcs.SetResult(null);
            });
        }
        else
        {
            Error = "Failed to retrieve Google play games authorization code";
            Debug.Log("Login Unsuccessful");
            tcs.SetException(new Exception("Failed"));
        }
    });
    return tcs.Task;
}


async Task SignInWithGooglePlayGamesAsync(string authCode)
{
    try
    {
        await AuthenticationService.Instance.SignInWithGooglePlayGamesAsync(authCode);
        Debug.Log($"PlayerID: {AuthenticationService.Instance.PlayerId}"); //Display the Unity Authentication PlayerID
        Debug.Log("SignIn is successful.");
    }
    catch (AuthenticationException ex)
    {
        // Compare error code to AuthenticationErrorCodes
        // Notify the player with the proper error message
        Debug.LogException(ex);
    }
    catch (RequestFailedException ex)
    {
        // Compare error code to CommonErrorCodes
        // Notify the player with the proper error message
        Debug.LogException(ex);
    }
}

已尝试的操作

  • 重新配置OAuth许可
  • 在安卓实体设备和模拟器测试
  • 将测试账号加入Google Play Games测试列表
  • 使用Google Play内部测试(对应SHA1密钥)
  • 测试Unity导出的APK(对应密钥库SHA1密钥)

使用版本

  • Unity:2022.3.18f1
  • GPS plugin:v11.01
  • Unity Auth:3.3.1

排查与解决思路

1. 补充错误状态日志

当前代码仅判断SignInStatus.Success,但success可能返回具体错误类型(如NetworkError、AccountPickerError等),修改LoginGooglePlayGames方法的失败分支,打印详细状态:

else
{
    Error = $"Failed to retrieve authorization code. Status: {success}";
    Debug.Log($"Login failed. Status: {success}");
    tcs.SetException(new Exception($"Failed with status: {success}"));
}

通过具体状态码快速定位是网络、账号还是配置问题。

2. 校验授权码回调结果

RequestServerSideAccess的回调中code可能为空,增加非空判断:

PlayGamesPlatform.Instance.RequestServerSideAccess(true, code =>
{
    if (string.IsNullOrEmpty(code))
    {
        Debug.Log("Authorization code is null or empty");
        Error = "Authorization code retrieval failed";
        tcs.SetException(new Exception("Authorization code is null"));
        return;
    }
    Debug.Log("Authorization code: " + code);
    Token = code;
    tcs.SetResult(null);
});

避免因空值导致后续认证流程异常。

3. 验证OAuth客户端ID配置

  • 确保Google Cloud Console中配置的Web应用客户端ID(非安卓客户端ID)已正确填入Unity项目的Services > Authentication > Google Play Games中
  • 确认OAuth许可范围包含https://www.googleapis.com/auth/games

4. 优化插件初始化逻辑

  • 开启插件调试日志,查看内部错误详情:
void Awake()
{
    var config = new PlayGamesClientConfiguration.Builder()
        .RequestServerAuthCode(false) // 根据需求调整,true需绑定Web客户端ID
        .Build();
    PlayGamesPlatform.InitializeInstance(config);
    PlayGamesPlatform.DebugLogEnabled = true;
    PlayGamesPlatform.Activate();
}

5. 编辑器环境特殊配置

  • 确保安装Google Play Games for Editor插件
  • 在Window > Google Play Games > Setup > Android Setup中配置测试账号,检查编辑器登录时是否有浏览器弹窗被拦截

6. 核对包名与密钥一致性

  • 确认Google Play Console中的应用包名与Unity导出包名完全一致
  • 检查Google Play Console中添加的SHA1指纹包含:Unity编辑器调试密钥、发布密钥库、内部测试版本密钥的SHA1

7. 版本兼容性排查

  • 尝试升级GPS plugin至最新稳定版,或回退到与Unity 2022.3兼容的版本(部分新版本可能存在适配问题)
  • 参考Unity官方文档,确认Unity Auth与GPS plugin版本兼容

内容的提问来源于stack exchange,提问作者Joshu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 06:12:04