Get a user ID from Google Play Services?
Asked Answered
S

4

10

Is there any way that we can get a Google Play ID (number, string, whatever) from a particular user when utilizing Google Play Services?

Sarson answered 16/10, 2014 at 20:45 Comment(0)
S
21

For Google Play Game Services you can use:

String playerId = Games.Players.getCurrentPlayerId(getApiClient());
Sankhya answered 10/1, 2015 at 17:12 Comment(2)
I figured it out, but thanks. This could be helpful to others in the future.Sarson
@Sankhya is it unique and constant? Can it be used for saving player's progress online?Fluff
S
4

It depends on the service you are trying to access. Here is the relevant class Player for Google Play Games: https://developer.android.com/reference/com/google/android/gms/games/Player.html

And the relevant class People from Google Plus: https://developer.android.com/reference/com/google/android/gms/plus/model/people/package-summary.html

Finally, there is AdvertisingIdClient for the Google Mobile Ads platform: https://developer.android.com/reference/com/google/android/gms/ads/identifier/AdvertisingIdClient.html

Shelly answered 16/10, 2014 at 20:58 Comment(8)
Yes, I don't want to ask the user for G+ permissions. I just want the user ID. I don't know how to get to this "Player" class.Sarson
com.google.android.gms.games.Player It's right on the first link.Shelly
So just import it and that's it? And I get the user's ID? Do I need to do anything else?Sarson
I notice that the player ID is a string, not an integer. What if the user didn't set a player string ID? I just want their user ID within Google Play Services....Sarson
Sounds like you want AdvertisingIdClientShelly
I do not. It needs to be persistent, in order to track all of a user's acquisitions (eg: free levels given for getting other users to play my game). The user can clear and reset the AdvertisingIdClient.Sarson
Then Player is what you want. To the best of my knowledge, it matches the account, and it is not user-set but tied to the google account.Shelly
Ok. So I know I need import com.google.android.gms.games.Player; and SOMETHING.getPlayerId();.. can you tell me: what is SOMETHING???Sarson
G
2

So the Games.Players class is deprecated and nobody gave a summarised solution to get the entire player object that allows to get the player ID, eventually. Here is what I've sorted out:

GoogleSignInAccount account;
PlayersClient playerClient;
Player player;
String playerId;

account = GoogleSignIn.getLastSignedInAccount(this);
if (account == null) {
    // do sign-in here
}

// Later:

playerClient = Games.getPlayersClient(this, account);
Task<Player> playerTask = playerClient.getCurrentPlayer();
player = playerTask.getResult();
if(player != null) {
    playerId = player.getPlayerId();
}
Gleason answered 1/10, 2018 at 15:4 Comment(0)
B
0

I believe, the answers above our outdated. Try this:

var playerID = PlayGamesPlatform.Instance.localUser.id
Blida answered 19/9, 2021 at 5:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.