Get account name / email from Google Drive Android API

I use the new Google Android API for Android , and I can connect and everything that it will allow me to do , but to use SpreadsheetService I need to extract accountName / email from a signed-in user.

How to do it?

Here’s the code where I am connecting the Google Drive API. It works great.

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, where I use the SpreadsheetService API , but I need an 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 tried this by adding this:

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

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

In addition, here are my rights:

 <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" /> 
+6
source share
3 answers

Now I'm back to the one below, which allows me to get an account at any time.

When I asked this question, I got the impression that I can only get it with the initial connection. Obviously, I was wrong.

 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 AndroidManifest:

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

You need to add:

  .addApi(Plus.API) 

when creating a client. Then this will work in onConnected(Bundle) :

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

if you already have the correct permission (what are you doing):

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

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(); 
0
source

All Articles