PGS 为 Android 玩家提供游戏身份,但此身份不一定是与用户关联的唯一身份。您可以同时使用 PGS、社交网络 ID 和您自己的游戏内 ID 系统为玩家提供登录选项。每个系统都能为您和玩家提供独特价值,所有系统可以协同运作,从而为 Android 玩家提供出色体验。
安全地检索并验证玩家身份,以进行后端集成
您可以请求服务器授权代码,以便服务器在获得已登录玩家的授权后通过 REST API 直接与 Play 游戏服务的服务器通信。这样一来,您就可以在获得授权后直接访问玩家的 ID、个人资料和其他信息(例如好友列表)。如果您在后端存储任何玩家 ID、好友 ID 或其他相关数据,就必须使用这种方法,以免出现在设备端操控这些字段的情况。
此外,对于某些较老的游戏和玩家,Android SDK 针对某个玩家返回的玩家 ID 可能与其他玩家在同一游戏中查看该玩家时看到的 ID 不同;在使用好友列表时尤为如此。但是,REST API 中返回的 player_id 始终是一致的,并且始终是其他玩家看到的 ID。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Sign-in\n\nFollowing the deprecation of the\n[Google Sign-In](https://android-developers.googleblog.com/2024/09/streamlining-android-authentication-credential-manager-replaces-legacy-apis.html)\nAPI, we are removing the games v1 SDK in 2026. After February 2025, you will be unable to publish\ntitles that are newly integrated with games v1 SDK, on Google Play. We recommend that you use the\ngames v2 SDK instead. \n\nWhile existing titles with the previous games v1 integrations continue to function for a\ncouple of years, you are encouraged to\n[migrate to v2](/games/pgs/android/migrate-to-v2)\nstarting June 2025. \n\nThis guide is for using the Play Games Services v1 SDK. For information\non the latest SDK version, see the\n[v2 documentation](/games/pgs/signin).\n\nPlay Games Services sign-in provides you with a player's gaming identity, which\nis a platform-level, gaming-specific identity for Android players. This identity\nhelps build a relationship between your game and the player. Players are more\nwilling to use this identity to sign in than with alternate centralized systems.\n\nIn addition, sign-in gives you a strong, consistent identifier that you can use as a key to a\ncloud save system. Cloud save is highly-valued; it enables players to do the\nfollowing:\n\n- Pick up where they left off if they change or reset their device\n- Play the game on multiple devices (for example, on another phone or a Chromebook)\n- Uninstall and later re-install the game\n\nYou can use Play Games Services identity as a key for your own cloud save\nsolution, or you can use the free [Saved Games service](/games/pgs/savedgames).\n\nPlay Games Services (PGS) supports automatic sign-in, a zero-friction way to\nsign in players, as well as manual sign-in, which is a one-click process. This\ndocument describes these different methods of sign-in.\n| **Note:** You can implement sign-in without implementing any other Play Games Services features (for example, leaderboards or saved games). Sign-in is a low-friction way to get a strong identity for your players.\n\nRecommended sign-in flow\n------------------------\n\nIn order to successfully sign players into your game, use the following\nsign-in flow:\n\n1. During the startup sequence of your game, try [Automatic sign-in](#automatic-sign-in) to automatically sign the player in.\n2. If automatic sign-in does not work and the user's device is online, try [Manual sign-in](#manual-sign-in).\n3. If manual sign-in is declined, remember the user's choice and do not show manual sign-in again, but continue to try Automatic sign-in on startup.\n\nAdditionally, you should ensure that users are able to sign into your game with\ntheir Play Games services account with a dedicated, easy-to-find button.\n\nAutomatic sign-in\n-----------------\n\nAutomatic sign-in allows users to sign into your game with zero friction - it's\nautomatic! If your game requests that the user sign in and the automatic sign-in\nsetting is enabled (in the Google Play Games app), then you will receive the\nuser's PGS identity credentials and the user will see the PGS sign-in experience.\n\nAutomatic sign-in is a setting users can control. Users can choose to\nautomatically sign into all games or be asked each time.\n| **Note:** Automatic sign-in only works if the game does not request additional OAuth Scopes. For more information, see [OAuth Scopes and PGS](#oauth-scopes).\n\nManual sign-in\n--------------\n\nIf a user doesn't automatically sign into the game, you can ask them to sign in\nmanually. Users can sign in with one click from a dialogue box displayed on top\nof your game.\n| **Note:** Manual sign-in works better if the game does not request additional OAuth Scopes. For more information, see [OAuth Scopes and PGS](#oauth-scopes).\n\nAllow PGS popups for the correct sign-in experience\n---------------------------------------------------\n\nWhen using PGS, users need to know they are signed in. This is done by showing\nthe PGS sign-in experience.\n\nTo show the sign-in experience, call the\n[`setViewForPopups()`](https://developers.google.com/android/reference/com/google/android/gms/games/GamesClient.html#setViewForPopups(android.view.View))\nmethod of the `GamesClient` class during game startup. This is critical to\nenable the correct user experience.\n\nOAuth Scopes and PGS\n--------------------\n\nPGS relies on the [OAuth\nsystem](https://developers.google.com/identity/protocols/OAuth2) to allow users\nto give your game access to their account.\n[OAuth Scopes](https://developers.google.com/identity/protocols/googlescopes)\nare permissions that you as a developer can request. If granted, these\npermissions let your game call certain APIs on behalf of the user and also give\nyou access to certain parts of their Google Account.\n\nPGS has a unique scope for games (`games-lite`) and relies on another scope\n(`drive.appdata`) if your game uses the saved games feature. The saved games\nfeature gives access to the user's Google Drive account, which is where the game\ndata is stored.\n\nYour game can request any scope that you need to create a great game experience\nfor your users. Your game can request all of the scopes it needs in one bundle,\nlimiting the number of requests for Google Account access. However, this\napproach has a major drawback. If you request more than the PGS-specific scopes,\nautomatic sign-in cannot succeed. Automatic sign-in is the best way to get\nplayers signed into your game.\n\nBest practice is to use incremental scope requests. Your game should ask for\nPGS-only scopes first in the initial request, and request other scopes\nseparately at a more relevant moment.\n\nOAuth access is granted using a different screen for each scope. This allows\nusers to decide, in a granular way, which scopes to give access to. By using\nautomatic sign-in with only PGS scopes, users will not have to interact with any\nOAuth prompts, reducing sign-in friction.\n\nSupport for multiple sign-in methods\n------------------------------------\n\nPGS provides a gaming identity for Android players, but it doesn't need to be\nthe only identity connected to your users. You can sign in players using PGS, a\nsocial network ID, and your own in-game ID system all at the same time. Each\nsystem provides unique value for both you and your players, and all can work\ntogether to make a great experience for Android players.\n\nSecurely retrieve and verify the player identity for backend integration\n------------------------------------------------------------------------\n\nYou can request a Server Auth code to enable your server\nto talk directly to the Play Games Services servers via the REST API with the\nauthority of the signed-in player. This enables direct access to the player's\nID, profile, and other information (such as friends list, if authorized). If you\nstore any Player IDs, friend IDs, or other related data in your backend, this\nmethod must be used to remove the potential for\ndevice-side manipulation of these fields.\n\nAdditionally, for some older games and players, the player ID returned by the\n*Android* SDK for a player may not be the same ID that other players see when\nviewing that player in the same game - this is particularly relevant when using\nthe friends list. However, the `player_id` returned within the REST API is\nalways consistent and is always the ID that is seen by other players.\n\nFor more information, see\n[Offline access](/games/pgs/v1/android/server-access) and the\n[REST API](/games/services/web/api/rest) documentation.\n\nClient implementation\n---------------------\n\nTo learn how to implement sign-in support on Android, see\n[Sign-in in Android Games](/games/pgs/v1/android/signin)."]]