androidserver-sidegoogle-play-gamesleaderboard

How to access a Google Play Games public leaderboard from a server without any user or client action?


I'm trying to implement leaderboards in my Android game using Google Play Games Services. I think I understand how to submit a score and access a leaderboard from a client which is signed in to Play Games, however I would also like to be able to access the leaderboard from my server. This way, I can show the leaderboard to users which are not yet signed in to Play Games or even show it on Discord with my bot. But I can't figure out how to do that.

Whenever I try to find information on the Internet about server-side access to Play Games, I only find how to get a one-time usage token from a client to then call Play Games from the server on behalf of the user (as I understand it).

But that's not what I want to do! I need to be able to access the leaderboard from my server as an admin (or as myself or as a service account or anything like that), not on behalf of a user. And I want to be able to do that at any time, not only when a client sends an access token...

Is this possible?

Thank you in advance for any help on this matter!


Solution

  • I created a new credential for a web server and then, using my Oauth consent screen, I managed to give myself a refresh token for my own Google account. This way I can get access tokens whenever I want from my server and access the Play Games REST API with these access tokens on behalf of my account.

    Here is how I did it step by step :

    1. In Play Console, create a new credential for a web server (if you don't already have one). In the associated Google Cloud Oauth client ID settings, add an "Authorized redirect URI". I don't think the URL matters but I would advice choosing one that you own... (and maybe it even works with one that doesn't exist but I'm not sure)
    2. Open this URL in a browser : https://accounts.google.com/o/oauth2/v2/auth?redirect_uri=<THE_URL_YOU_CHOSE_ABOVE>&prompt=consent&response_type=code&client_id=<YOUR_CLIENT_ID>&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fgames+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.appdata+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fgames&access_type=offline
      After accepting the prompt that should be displayed, you will be redirected to the URL you chose and you should see a parameter 'code=...' (at the top of your screen where you see the URL). This is the authorization code you need!
    3. Exchange that code for a refresh token (Step 5 in the documentation).
    4. Whenever needed, get a new access token using the refresh token ("Refreshing an access token" section in the documentation).
    5. The access token can be used in the authorization header of your requests to the Play Games REST API :)

    The whole procedure is explained here : https://developers.google.com/identity/protocols/oauth2/web-server