从Google Drive Android API获取帐户名称/电子邮件

时间:2014-04-04 14:30:56

标签: android google-spreadsheet-api google-drive-android-api

我正在使用新的Google Drive Android API,我可以连接,并执行all that it will allow me to do,但为了使用SpreadsheetService,我需要从已登录的用户中提取accountName / email。< / p>

我该怎么做?

以下是我连接Google Drive API的代码。它工作正常。

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...");
        }

    }
}

以下是我使用SpreadsheetService API的地方,但我需要帐户名来获取令牌

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

我试过这个添加:

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

...在onConnected方法中,但它会抛出NullPointerException

此外,这是我的权限:

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

3 个答案:

答案 0 :(得分:14)

您需要添加:

                    .addApi(Plus.API)

构建客户端时。然后,这将在onConnected(Bundle)

中有效
String accountName = Plus.AccountApi.getAccountName(mGoogleApiClient);

假设您已拥有正确的权限(您这样做):

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

答案 1 :(得分:4)

我现在已经恢复到以下版本,这使我可以随时获得该帐户。

当我问这个问题时,我的印象是我只能在最初连接时才能得到它。显然我不对。

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;
}

当然,包括AndroidManifest中的以下权限:

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

答案 2 :(得分:1)

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