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

使用play-games-plugin-for-unity时服务器授权遇redirect_uri_mismatch错误

Fixing redirect_uri_mismatch for Play Games Plugin for Unity + Flask Server Authentication

I’ve run into this exact issue before when working with the Play Games Plugin for Unity and server-side OAuth validation—let’s break down what’s going wrong and how to fix it quickly:

The Root Cause

When you use the Play Games Plugin to get a server auth code (the code you’re sending to your Flask server), Google binds that code to a specific redirect_uri behind the scenes. This isn’t something the Unity client sends explicitly—it’s hardcoded by the plugin to urn:ietf:wg:oauth:2.0:oob for server-side code exchange. Your mistake was using the oauth2client default (postmessage) and deleting the authorized redirect URIs in the Play Games Console.


Step 1: Fix Your Play Games Console Configuration

  1. Go to the Google Play Games Console and open your game.
  2. Navigate to Linked apps > Web (the web app you linked to your game).
  3. In the Authorized redirect URIs section, add urn:ietf:wg:oauth:2.0:oob (if you deleted it earlier, re-add it). Don’t remove any auto-generated URIs here unless you’re sure you don’t need them—just add this one.
  4. Save your changes (it might take a few minutes for the config to propagate).

Step 2: Update Your Flask Server Code

You need to explicitly pass the correct redirect_uri to the credentials_from_code method—don’t rely on the default. Here’s the fixed code:

from oauth2client import client

# Make sure these are the Client ID/Secret from your LINKED WEB APP (not the Android app)
CLIENT_ID = LINKED_WEB_APP_CLIENT_ID
CLIENT_SECRET = LINKED_WEB_APP_CLIENT_SECRET
SCOPES = ['https://www.googleapis.com/auth/games']

credentials = client.credentials_from_code(
    CLIENT_ID,
    CLIENT_SECRET,
    SCOPES,
    server_auth_code,
    redirect_uri='urn:ietf:wg:oauth:2.0:oob'  # This is the critical missing parameter
)

Step 3: Double-Check Your Unity Client Code

Ensure you’re actually fetching a server auth code (not a client-side ID token) in Unity. The correct method from the plugin is:

// After the user is authenticated
string serverAuthCode = PlayGamesPlatform.Instance.GetServerAuthCode();
// Send this code to your Flask server

If you’re using any other method to get a code, it won’t work for server-side exchange.


Why This Works

The Play Games Plugin for Unity uses the "out-of-band" (urn:ietf:wg:oauth:2.0:oob) redirect URI pattern for server auth codes because there’s no browser redirect happening in the mobile app context. Google expects your server to use this exact URI when exchanging the code, and the console must have it listed as an authorized redirect URI to prevent mismatch errors.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 05:09:16