【问题标题】:Get Account Name / Email from Google Drive Android API从 Google Drive Android API 获取帐户名称/电子邮件
【发布时间】:2014-04-04 14:30:55
【问题描述】:

我正在使用新的Google Drive Android API,我可以连接并执行all that it will allow me to do,但为了使用电子表格服务,我需要从登录用户中提取帐户名/电子邮件。

我该怎么做?

这是我连接 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" />

【问题讨论】:

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


【解决方案1】:

你需要添加:

                    .addApi(Plus.API)

在构建客户端时。然后这将在onConnected(Bundle) 中工作:

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

假设您已经拥有正确的权限(您已经拥有):

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

【讨论】:

  • 是否可以不使用 Plus API?只使用 com.google.android.gms:play-services-identity?
  • Plus 现已弃用。
【解决方案2】:

我现在已恢复到下面的内容,这使我可以随时获取帐户。

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

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

【讨论】:

  • 如果您有多个谷歌帐户怎么办?然后你就自动选择第一个。
  • 我同意 Roel 的观点,如果您有多个帐户怎么办?这是我发现的与我面临的同样问题的唯一主题。我需要 Google Drive 登录用户的电子邮件。我们是否已经有了真正的解决方案?
  • @LucasEduardo 你找到解决方案了吗?
  • 不管怎样,我现在正在使用 Google EasyPermissions。这会给用户一个弹出窗口来选择他们想要使用的帐户。 developers.google.com/sheets/api/quickstart/android
  • 哦,我正在使用弹出窗口询问用户他想选择哪个帐户,正如 LimaNightHawk 上面所说的那样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-08
  • 1970-01-01
  • 2020-06-07
  • 2019-07-12
  • 1970-01-01
相关资源
最近更新 更多