1

Is there a way to get the token that was used to log in the user with Google Play Game Services?

I'm looking for something like:

@Override
public void onSignInSucceeded() {
    String email = getGamesClient().getCurrentAccountName();
    String token = getGamesClient().getToken();
}

I need this to authenticate the user when they are contacting my own server.

maclir
  • 3,218
  • 26
  • 39

1 Answers1

1

This is how I managed to get the token:

@Override
public void onSignInSucceeded() {
    String email = getGamesClient().getCurrentAccountName();
    String scopes = getScopes();
    new registerBackground(getApplicationContext()).execute(email, scopes);
}


private class registerBackground extends AsyncTask<String, Void, Void> {
    Context context;
    registerBackground (Context context) {
        this.context = context;
    }

    @Override
    protected Void doInBackground(String... params) {
        try {
            String oAuthToken = GoogleAuthUtil.getToken(context, params[0], params[1]);
            ...
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    ...
}
maclir
  • 3,218
  • 26
  • 39
  • Thanks for sharing, works perfectly. Did you implement an up to date version at all, as Google says the `GoogleAuthUtil.getToken(...)` approach is now unsafe :-( I don't need to access Google API's server side, just to verify the token, so maybe it is safe for this purpose? –  Oct 16 '16 at 07:10
  • Unsafe: https://developers.google.com/identity/sign-in/android/migration-guide#migrate_from_the_googleauthutil_server_auth_code_flow ... ? –  Oct 16 '16 at 07:23
  • @poirot Have not spent any time on it yet. Please update here if you have any success with the new approach. :) – maclir Oct 18 '16 at 12:45
  • See my own answer to my own question here (http://stackoverflow.com/questions/40069681/should-i-use-googleauthutil-gettoken-or-not) I found the solution at last ! Took me a couple of days to figure out :-D –  Oct 18 '16 at 14:20