Server-side access to Google Play Games Services
Stay organized with collections
Save and categorize content based on your preferences.
We recommend that you use PgsGamesSignInClient
to authenticate players and securely pass the player's identity to the backend
server. This enables your game to securely retrieve the player's identity and
other data without being exposed to potential tampering while passing through
the device.
Once the player authenticates successfully, you can request a special single-use
code (called the server auth code) from the Play Games Services v2 Native SDK (beta), which
the client passes to the server. Then, on the server, exchange the server auth
code for an OAuth 2.0 token that the server can use to make calls to the
Google Play Games Services API.
For additional guidance on adding authentication in your games, see
Platform authentication.
The following steps are required for offline access:
In the Google Play Console: Create a credential for your game server.
The OAuth client type of the credential will be "web".
In the Android app: As part of platform authentication, request a server
auth code for your server's credential, and pass that to your server. The
PgsGamesSignInClient can request three OAuth 2.0 scopes when requesting
server-side access to Play Games Services web APIs. The optional scopes are
PGS_AUTH_SCOPE_EMAIL, PGS_AUTH_SCOPE_PROFILE, and
PGS_AUTH_SCOPE_OPENID. The two default scopes are DRIVE_APPFOLDER and
GAMES_LITE.
On your game server: Exchange the server auth code for an OAuth access
token using Google auth services, and then use this to call the
Play Games Services REST APIs.
Google Play Game services does not provide backend
support for Web games. However, it does provide backend server support
for your Android game's server.
Be sure that you use the OAuth Client ID registered for your game
server and not the OAuth Client ID of your Android application.
(Optional) If your game server requires offline access (long lived access
using a refresh token) to Play Games Services, you can set the
force_refresh_token parameter to true.
(Optional) As part of the authentication, new users should encounter a
single consent screen for additional scopes. Upon accepting the consent, you
set the PgsAuthScopescopes parameter with PGS_AUTH_SCOPE_EMAIL,
PGS_AUTH_SCOPE_PROFILE, and PGS_AUTH_SCOPE_OPENID OAuth scopes. If users
decline the consent, only the two default scopes DRIVE_APPFOLDER and
GAMES_LITE are sent to the backend.
//#include "google/games/pgs_games_sign_in_client.h"//1.DefinetheCallback//Thisfunctioniscalledwhentheserver-sideaccessrequestcompletes.//Itprovidestheauthorizationcode(onsuccess)oranerror(onfailure).voidOnServerSideAccessCallback(void*context,PgsErrorerror,constchar*serverAuthCode){if(error==PgsError_Success){if(serverAuthCode!=nullptr){__android_log_print(ANDROID_LOG_INFO,"Games","Received Server Auth Code: %s",serverAuthCode);//Send'serverAuthCode'toyourbackendserverimmediately.//YourserverwillexchangethiscodeforanOAuthaccesstoken.}}else{__android_log_print(ANDROID_LOG_ERROR,"Games","Failed to get server auth code. Error: %d",error);}}//2.DefinetheWrapperFunctionvoidRequestServerAccess(PgsGamesSignInClient*signInClient){if(signInClient==nullptr){return;}//Thismustmatchthe"Web client ID"fromyourGoogleCloudConsole//(linkedtoyourPlayConsoleGameServerCredential).constchar*SERVER_CLIENT_ID="xxxx";//Setto'true'ifyourserverneedsaRefreshToken(long-livedaccess).//Setto'false'ifyouonlyneedanAccessToken(short-lived).boolforceRefreshToken=false;//CalltheAPIPgsGamesSignInClient_requestServerSideAccess(signInClient,SERVER_CLIENT_ID,forceRefreshToken,OnServerSideAccessCallback,//Thecallbackdefinednullptr//Usercontext(optional,passedtocallback));}//3.ExampleUsagevoidTriggerSignInProcess(PgsGamesClient*gamesClient){//ObtaintheSign-InClientfromthemainGamesClientPgsGamesSignInClient*signInClient=PgsGamesClient_getSignInClient(gamesClient);RequestServerAccess(signInClient);}
Send the OAuth auth code token to your backend server so it may be exchanged,
the Player ID verified against the Play Games Services REST APIs, and then
authenticated with your game.
Send the server auth code
Send the server auth code to your backend server to exchange for access and
refresh tokens. Use the access token to call the Play Games Services API on
behalf of the player and, optionally, store the refresh token to acquire a new
access token when the access token expires.
The following code snippet shows how you might implement the server-side code in
the C++ programming language to exchange the server auth code for access
tokens.
You can retrieve the OAuth scopes using the
Google API Client Libraries
in Java or Python to get the GoogleIdTokenVerifier object. The following code
snippet shows the implementation in Java programming language.
Call
Achievements.increment
to report progress on an achievement, and find out if the player unlocked it.
If you are debugging a game that hasn't reached production, you can call
Achievements.reset
or
Achievements.resetAll
from the Management APIs to reset achievements to their original state.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2026年06月16日 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2026年06月16日 UTC."],[],[]]