Get Account Name / Email from Google Drive Android API

I’m using the new Google Drive Android API and I can connect, and do all that it will allow me to do, but in order to use the SpreadsheetService, I need to extract the accountName/email from the signed in user.

How do I do that?

  • Get rid of Android navigation drawer translucency
  • Jelly Bean WebView not working well with HTML maxlength attribute for text box
  • How to Prevent an Alert Dialog Getting Closed by Back Button
  • How to disable cursor positioning and text selection in an EditText? (Android)
  • How to wrap DrawerLayout around PreferenceActivity in an Android app?
  • Run android program as root
  • Here’s the code where I connect the Google Drive API. It works fine.

    private void connect() {
    
        if (isGooglePlayServicesAvailable()) {
            if (mGoogleApiClient == null) {
                mGoogleApiClient = new GoogleApiClient.Builder(this)
                        .addApi(Drive.API)
                        .addScope(Drive.SCOPE_FILE)
                        .addConnectionCallbacks(this)
                        .addOnConnectionFailedListener(this)
                        .build();
            }
            // And, connect!
    
            if (!mGoogleApiClient.isConnecting() && !mGoogleApiClient.isConnected()) {
                mGoogleApiClient.connect();
                writeLog("Connecting...");
            }
    
        }
    }
    

    Here’s where I’m using the SpreadsheetService API, but I need the account name to get the token.

    String accountName = "???"; // how do I get this From GoogleClientApi / mGoogleApiClient ?
    String accessToken = GoogleAuthUtil.getTokenWithNotification(GDriveActivity.this, accountName, scope, null);
    SpreadsheetService service = new SpreadsheetService("v1");
    

    I have tried this adding this:

    String accountName = Plus.AccountApi.getAccountName(mGoogleApiClient);
    

    … in the onConnected method, but it throws a NullPointerException.

    Also, here are my permissions:

    <uses-permission
        android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission
        android:name="android.permission.VIBRATE" />
    <uses-permission
        android:name="android.permission.INTERNET" />
    <uses-permission
        android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission
        android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission
        android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission
        android:name="android.permission.NETWORK" />
    <uses-permission
        android:name="android.permission.USE_CREDENTIALS" />
    

    Related posts:

    android black color at 85% opacity
    Is there any free C++ and/or C# compiler that runs on an Android-enabled Tablet PC?
    Firebase Storage - StorageReference.getFile() crashes when device is offline
    Prevent Android WebView caching data
    Set weight (percentage) programmatically
    Android Studio 0.4.3 - Task &#039;assemble&#039; not found in root project
  • Android Emulator: This app won't run without Google Play services
  • Lock the android device programmatically
  • Use C++ with Android ndk/jni
  • Double parameter with 2 digits after dot in strings.xml?
  • Incorrect presentation timestamps when seeking backwards through MP4 video
  • Change project file structure after migrating from Eclipse to Android Studio gradle structure
  • 3 Solutions collect form web for “Get Account Name / Email from Google Drive Android API”

    You need to add:

                        .addApi(Plus.API)
    

    when building the client. Then this will work in onConnected(Bundle):

    String accountName = Plus.AccountApi.getAccountName(mGoogleApiClient);
    

    assuming you already have the correct permission (which you do):

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

    I’ve now reverted to the below which allows me to get the account at any time.

    When I asked this question I was under the impression that I could only get it when initially connecting. Obviously I was incorrect.

    private String getAccountName() {
    
        String accountName = null;
    
        AccountManager manager = (AccountManager) getSystemService(ACCOUNT_SERVICE);
        Account[] list = manager.getAccounts();
        for (Account account : list) {
            if (account.type.equalsIgnoreCase("com.google")) {
                accountName = account.name;
                break;
            }
        }
        return accountName;
    }
    

    Of course, including the following permission in the AndroidManifest:

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

    You can get various data from Plus.PeopleApi

    protected static GoogleApiClient mGoogleApiClient;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(Plus.API).addScope(Plus.SCOPE_PLUS_LOGIN)
                .addApi(Games.API).addScope(Games.SCOPE_GAMES)
                .build();
    }
    
    @Override
    public void onConnected(Bundle bundle) {
        String data =  Plus.API.getName() + "\n" +
                Plus.PeopleApi.getCurrentPerson(mGoogleApiClient).getName() + "\n" +
                Plus.PeopleApi.getCurrentPerson(mGoogleApiClient).getId() + "\n" +
                Plus.PeopleApi.getCurrentPerson(mGoogleApiClient).getAboutMe() + "\n" +
                Plus.PeopleApi.getCurrentPerson(mGoogleApiClient).getBirthday() + "\n" +
                Plus.PeopleApi.getCurrentPerson(mGoogleApiClient).getBraggingRights() + "\n" +
                Plus.PeopleApi.getCurrentPerson(mGoogleApiClient).getCurrentLocation() + "\n" +
                Plus.PeopleApi.getCurrentPerson(mGoogleApiClient).getNickname() + "\n" +
                Plus.PeopleApi.getCurrentPerson(mGoogleApiClient).getDisplayName() + "\n" +
                Plus.PeopleApi.getCurrentPerson(mGoogleApiClient).getTagline();
        Toast.makeText(this, data, Toast.LENGTH_LONG).show();
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.