Skip to main content
本教程指导你完成使用 Google 和 HTML5/JavaScript 进行 PlayFab 身份验证的过程。

要求

你需要:

服务器和域名

本指南需要具有有效域名的服务器来跟进。如果你还没有注册域名和远程 Web 服务器,请按照我们的为测试运行 HTTP 服务器教程,了解如何设置一个具有有效域名的本地 Web 服务器。 在本指南中,我们假设你的域名是 [http://playfab.example](http://playfab.example)

注册 Google API 项目

首先导航到 Google API Console
  1. 导航到 CREATE PROJECT 并选择它。
Google API Manager create new credentials
  1. 填写 Project Name 下的字段。
  2. 选择 Create 按钮。
Google API Manager new project
API Manager 生成项目需要 10-20 秒是正常的。
创建项目后:
  1. 导航到 OAuth consent screen
  2. 在 User Type 下选择 External
  3. 然后选择 Create
Google API Manager OAuth consent screen
  1. 填写 App Name
  2. 确保有 user support emailDeveloper Contact Email 的电子邮件
  3. 然后选择 SAVE AND CONTINUE
Google API Manager OAuth Consent Screen configuration Google API Manager OAuth Consent Screen configuration 2
  1. 接下来,导航到 Credentials
  2. 选择 Create credentials
  3. 然后选择 OAuth client ID
Google API Manager open OAuth credentials
  1. 选择 Web Application 作为你的 Application type
  2. 为你的应用程序命名 Name
  3. 将你的域名添加到 Authorized JavaScript origins —— 在我们的情况下是 [PlayFab](http://playfab.example.com)
  4. 最后,选择 Create 按钮提交你的更改。
Google API Manager create OAuth credentials 在下面所示的 OAuth client 屏幕上,Google API Manager 显示了两个重要信息:
  1. client ID
  2. client secret
请务必将这些值复制并保存在易于访问的安全位置,因为它们将在本教程稍后显示的授权过程中使用。
Google API Manager OAuth client ID and secret 转到你 title 的 PlayFab Game Manager 页面。
  1. 在菜单中导航到 Add-ons
  2. 找到并打开 Google Add-on 图标/链接。
PlayFab Game Manager Add-on
  1. 填写 Client ID
  2. 填写 Client secret
  3. 然后选择 Install Google 按钮。
PlayFab Game Manager Install Google Add-on
截至 2017 年 7 月,Google API Manager正确挂钩 Allowed JS 源域的坏习惯。如果你收到以下错误:“idpiframe_initialization_failed”, details: “Not a valid origin for the client: somedomain.com…”,请删除凭据并重新创建它们。无需删除整个项目——只需删除凭据即可。

使用访问令牌进行测试

在此示例中,我们展示如何使用经典访问令牌方法测试 LoginWithGoogleAccount API。使用下面提供的 HTML 代码进行测试。
请务必*用你自己的值替换 YOUR_CLIENT_IDYOUR_PLAYFAB_TITLE
在 TypeScript 类型定义文件中,LoginWithGoogleAccountRequest 对象中缺少 AccessToken 参数。如果你在 Node SDK 中使用 TypeScript,你可以创建一个自定义类型 interface LoginRequest extends PlayFabClientModels.LoginWithGoogleAccountRequest { AccessToken: string; },并在使用 API 时进行类型转换 PlayFabClient.LoginWithGoogleAccount( { AccessToken: accessToken, CreateAccount: true, } as LoginRequest, onPlayFabResponse);
使用(已弃用的)Google Sign-in Platform library 进行测试
下面的示例使用 Google Sign-In platform library。根据 Google Identity 公共文档站点,用于 Web 的 Google Sign-In JavaScript platform library 将于 2023 年 3 月 31 日弃用。此示例将不再适用于新创建的 Google 客户端 —— 请参考先前的代码示例,它使用较新的 Google Identity Services 库!
记住使用你的 Web 服务器打开此页面,并确保使用你在配置 Google Project 时指定的 URL(在我们的情况下是 http://playfab.example)访问此页面。
  1. 页面打开后,选择 G Signed In,并按照通用的 Google 身份验证流程。
  2. 完成后,脚本将尝试在 PlayFab 端进行身份验证并输出结果。
Google Auth Example
如果你已经有一个 Google 身份验证会话正在运行,你就不必选择 G Signed In。一切都会自动发生。
最后修改于 2026年8月13日