使用新版Google云端硬盘API检索帐户名称

时间:2014-02-01 18:26:38

标签: android google-drive-api google-play-services

我按照https://developers.google.com/drive/android/auth

中的说明设置了Google Play服务的授权流程

现在用户已授权我想要检索帐户名称的应用。但我在API(http://developer.android.com/reference/gms-packages.html)中找不到任何有用的方法。

3 个答案:

答案 0 :(得分:2)

我假设你已经有QUICKSTARTDEMO,或类似的正常运行,所以我将参考这两个例子。在BaseDemoActivity.java代码中,您会注意到在连接失败时会调用帐户选择

@Override public void onConnectionFailed(ConnectionResult result) {
  ...
  result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
  ...
}

......它又回到了onActivityResult()。我只是获取了意图数据并获取了KEY_ACCOUNT_NAME,这是所选的电子邮件。下面的代码是从DEMO的BaseDemoActivity.java(我在上面提到)中修改的。

@Override protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
  switch (requestCode) {
    case REQUEST_CODE_RESOLUTION:
    if ((resultCode == RESULT_OK) && (data != null) && (data.getExtras() != null ))
      // user selected account, get it
      String email = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
    else
      finish();    // user cancelled selection, an easy solution
    break;
  }

希望它有所帮助。

答案 1 :(得分:0)

看起来数据 Intent始终 null :(

public void startResolutionForResult(Activity var1, int var2) throws SendIntentException {
    if(this.hasResolution()) {
        var1.startIntentSenderForResult(this.mPendingIntent.getIntentSender(), var2, (Intent)null, 0, 0, 0);
    }
}

此代码来自ConnectionResult.class

答案 2 :(得分:0)

  1. 检查用户是否签名

    GoogleSignInClient mGoogleSignInClient;
        private void setupAuthorization() {
            GoogleSignInOptions signInOptions =
                    new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                            .requestEmail() // add it if you want to user email 
                            .requestScopes(Drive.SCOPE_FILE)
                            .build();
            mGoogleSignInClient = GoogleSignIn.getClient(this, signInOptions);
            mGoogleSignInClient.silentSignIn().addOnSuccessListener(googleSignInAccount -> {googleSignInAccount.getEmail()}).addOnFailureListener(e -> {});
    }
    
  2. 如果要在登录后获取电子邮件和姓名

    @Override                 受保护的void onActivityResult(最终int requestCode,最终int resultCode,最终Intent数据){                     super.onActivityResult(requestCode,resultCode,data);                     开关(requestCode){                         情况REQUEST_CODE_DRIVE_SIGN_IN:                             Log.i(TAG,“登录请求代码”);                         //在用户登录后调用。                         如果(resultCode == RESULT_OK){                             Log.i(TAG,“已成功登录。”);                             //在这里使用最后一个登录帐户,因为该帐户已经具有云端硬盘范围。     GoogleSignInAccount googleSignInAccount = GoogleSignIn.getLastSignedInAccount(this);     googleSignInAccount.getEmail();                             mDriveClient = Drive.getDriveClient(this,GoogleSignIn.getLastSignedInAccount(this));                             //构建驱动器资源客户端。                             mDriveResourceClient =                                     Drive.getDriveResourceClient(this,GoogleSignIn.getLastSignedInAccount(this));                         }                         break;}}

可以找到详细信息here