Pregunta

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?

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" />
¿Fue útil?

Solución 2

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" />

Otros consejos

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" />
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top