0

I am trying to find the latest solution with GoogleApiClient.Plus.API (android) for getting the email for the gplus profile. In internet, stackoverflow, every example found is obsolete and deprecated and of no use.

If it can be fetched only from Auth.GOOGLE_SIGN_IN_API then is it a two step process to fetch half of info from Auth API and rest from Plus.API ??

Thanks in advance for answer.

user2746732
  • 119
  • 1
  • 9

3 Answers3

0

First I assume you have a button for google plus sign in

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.sign_in_button:
             Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
             startActivityForResult(signInIntent, RC_SIGN_IN);
             break;
        }
}

At your onActivityResult you will catch sign in result

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

    // Result returned from launching the Intent from and you can extract your user information there
    if (requestCode == RC_SIGN_IN) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        if (result.isSuccess()) {
        // Signed in successfully, show authenticated UI.
        GoogleSignInAccount acct = result.getSignInAccount();
        //This line is your need
        String yourEmail = acct.getEmail();
        }
    }
}
Emin Ayar
  • 1,104
  • 9
  • 13
  • No I dont want to use this GoogleSignInApi as I have to pull some other information from gplus api. So do you suggest to call both api one after another. I think there should be a way to get the email from Person or GPlus Profile apis. – user2746732 Apr 05 '16 at 17:12
0

Here is how to get the email that is associated with the device, and probably with your app too:

Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
Account[] accounts = AccountManager.get(context).getAccounts();
for (Account account : accounts) {
    if (emailPattern.matcher(account.name).matches()) { //If email matches the account associated with the device
        String possibleEmail = account.name;
        ...
    }
}

Let me know if this worked

Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83
  • I am trying to use as below m_GoogleApiClient = new GoogleApiClient.Builder(m_activity) .enableAutoManage(m_activity, this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(Plus.API) .addScope(Plus.SCOPE_PLUS_LOGIN) .build(); m_GoogleApiClient.connect(); – user2746732 Apr 05 '16 at 17:46
0

After two days of struggle at last this post saved my life .. How to get profile like gender from google signin in Android?

But the apis are obsolete, did a bit of scratch head and could make it work.

create your google api client like this

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .requestScopes(new Scope(Scopes.PLUS_LOGIN))
                .build();

        m_GoogleApiClient = new GoogleApiClient.Builder(m_activity)
                .enableAutoManage(m_activity, this)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .addApi(Plus.API)
                .build();

then on onActivityResult()

GoogleSignInResult result =   Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        Log.d(TAG, "handleSignInResult:" + result.isSuccess());
        if (result.isSuccess()) {
            GoogleSignInAccount acct = result.getSignInAccount();
            fetchConnectedProfileInfo();
        } 

public void fetchConnectedProfileInfo()
{
    Log.d(TAG, "fetchConnectedProfileInfo");
    if (m_GoogleApiClient.hasConnectedApi(Plus.API)) {
        Plus.PeopleApi.load(m_GoogleApiClient, "me").setResultCallback(this);
    }
}

Refer my github page for full code sample https://github.com/sandipsahoo2k2/social-login

Community
  • 1
  • 1
user2746732
  • 119
  • 1
  • 9