Accessing Google Account Id /username via Android
Asked Answered
D

7

79

How do you access the user's Google Account Id / username in code? I am building an application that will call a web service to store data and I want to identify the identity of the person submitting the data.

Duroc answered 11/2, 2010 at 15:38 Comment(2)
Get Google Account Username and Email https://mcmap.net/q/11433/-get-gmail-profile-image-and-full-nameKaliningrad
There is also a library for account management in android here.Janettjanetta
B
47

I've ran into the same issue and these two links solved for me:

The first one is this one: How do I retrieve the logged in Google account on android phones?

Which presents the code for retrieving the accounts associated with the phone. Basically you will need something like this:

AccountManager manager = (AccountManager) getSystemService(ACCOUNT_SERVICE);
Account[] list = manager.getAccounts();

And to add the permissions in the AndroidManifest.xml

<uses-permission android:name="android.permission.GET_ACCOUNTS"></uses-permission>
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"></uses-permission>

Additionally, if you are using the Emulator the following link will help you to set it up with an account : Android Emulator - Trouble creating user accounts

Basically, it says that you must create an android device based on a API Level and not the SDK Version (like is usually done).

Barren answered 10/9, 2011 at 14:4 Comment(4)
how can I get the account avatar ? Thank you so much`Hephzipa
@Hephzipa this is a little bit out of the scope of this question, but I believe this other one could help ya. #17905566Barren
A bit late but this may help someone if you trying to get account's avatar (#9129200)Woke
There is also a library for account management in android here.Janettjanetta
W
10

This Method to get Google Username:

 public String getUsername() {
    AccountManager manager = AccountManager.get(this);
    Account[] accounts = manager.getAccountsByType("com.google");
    List<String> possibleEmails = new LinkedList<String>();

    for (Account account : accounts) {
        // TODO: Check possibleEmail against an email regex or treat
        // account.name as an email address only for certain account.type
        // values.
        possibleEmails.add(account.name);
    }

    if (!possibleEmails.isEmpty() && possibleEmails.get(0) != null) {
        String email = possibleEmails.get(0);
        String[] parts = email.split("@");
        if (parts.length > 0 && parts[0] != null)
            return parts[0];
        else
            return null;
    } else
        return null;
}

simple this method call ....

And Get Google User in Gmail id::

 accounts = AccountManager.get(this).getAccounts();
    Log.e("", "Size: " + accounts.length);
    for (Account account : accounts) {

        String possibleEmail = account.name;
        String type = account.type;

        if (type.equals("com.google")) {
            strGmail = possibleEmail;

            Log.e("", "Emails: " + strGmail);
            break;
        }
    }

After add permission in manifest;

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
Wind answered 11/8, 2014 at 10:34 Comment(1)
Why interenet and access network state permissions are required?Unmoving
D
6

Retrieve profile information for a signed-in user Use the GoogleSignInResult.getSignInAccount method to request profile information for the currently signed in user. You can call the getSignInAccount method after the sign-in intent succeeds.

GoogleSignInResult result = 
Auth.GoogleSignInApi.getSignInResultFromIntent(data);
GoogleSignInAccount acct = result.getSignInAccount();
String personName = acct.getDisplayName();
String personGivenName = acct.getGivenName();
String personFamilyName = acct.getFamilyName();
String personEmail = acct.getEmail();
String personId = acct.getId();
Uri personPhoto = acct.getPhotoUrl();
Dent answered 9/5, 2017 at 11:33 Comment(0)
B
2
if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
  Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
  String userid=currentPerson.getId(); //BY THIS CODE YOU CAN GET CURRENT LOGIN USER ID
}
Burne answered 16/9, 2015 at 5:38 Comment(2)
What lib are you using, I get Plus Cannot resolve symbol ?Washbowl
This is based on using the Google+ API which is probably too exotic for most developersAbelabelard
X
2

Used these lines:

AccountManager manager = AccountManager.get(this);
Account[] accounts = manager.getAccountsByType("com.google");

the length of array accounts is always 0.

Xerosere answered 9/4, 2018 at 14:33 Comment(1)
Please use android device below Android 6.0 - 6.… (API levels 23-…) , because higher version device required run-time permissions and it will always return length 0.Approve
F
0

There is a sample from google, which lists the existing google accounts and generates an access token upon selection , you can send that access token to server to retrieve the related details from it to identify the user.

You can also get the email id from access token , for that you need to modify the SCOPE

Please go through My Post

Frontwards answered 23/4, 2013 at 15:2 Comment(0)
T
-4
String name = android.os.Build.USER;

if (!TextUtils.isEmpty(name)) {
    nameEdit.setText(name);
}
Thermocouple answered 22/4, 2015 at 14:14 Comment(1)
Well, I tried this and it returned me "dpi". Clearly not the user's name.Ungotten

© 2022 - 2024 — McMap. All rights reserved.