简体   繁体   中英

Google Login: Getting User information with play services

I am working on an android game where I have to get the users gmail address for the back-end. I am using the game helper class that google provides and am using the google play API and well as the plus api. Until recently I have used

Plus.AccountApi.getAccountName(_gameHelper.getApiClient());

In order to get the address, but the this code is depreciated so I can not user it on certain devices.

I have tried getting the information from the information from onActivityResult from the login but the Intent returns as null. Not only that but to access that information from the Intent I require the google sign in API which will not function with the play API. I would appreciate any help or suggestions on this matter. Thank You very much in advance.

Add Google Sign-In to Your Android App

Configure Google Sign-In:

// Configure sign-in to request the user's ID, email address, and basic profile. ID and
// basic profile are included in DEFAULT_SIGN_IN.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
    .requestEmail()
    .build();

// Build a GoogleApiClient with access to GoogleSignIn.API and the options above.
mGoogleApiClient = new GoogleApiClient.Builder(this)
    .enableAutoManage(this, this)
    .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
    .build();

Then, when the sign-in button is clicked, start the sign-in intent:

Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);

The user is prompted to select a Google account to sign in with. If you requested scopes beyond profile, email, and openid, the user is also prompted to grant access to the requested resources.

Finally, handle the activity result:

@Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Result returned from launching the Intent from
    //   GoogleSignInApi.getSignInIntent(...);
    if (requestCode == RC_SIGN_IN) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        if (result.isSuccess()) {
            GoogleSignInAccount acct = result.getSignInAccount();
            // Get account information
            mFullName = acct.getDisplayName();
            mEmail = acct.getEmail();
        }
    }
}

See: https://developers.google.com/identity/sign-in/android/

This is how you do it in Kotlin in the current version:

Games.getPlayersClient(requireContext(), GoogleSignIn.getLastSignedInAccount(requireContext())!!)
            .currentPlayer
            .addOnSuccessListener { player ->
                
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM