在不使用newChooseAccountIntent()的情况下设置Google帐户

时间:2017-05-15 07:02:29

标签: android google-drive-api google-drive-android-api accountmanager google-account

我想为 GoogleAccountCredential.usingOAuth2 设置Google帐户,但不想使用它的newChooseAccountIntent方法来设置帐户。

代码: -

private static final String[] SCOPES = {DriveScopes.DRIVE_FILE, DriveScopes.DRIVE_APPDATA, DriveScopes.DRIVE_METADATA_READONLY};

mCredential = GoogleAccountCredential.usingOAuth2(
                getApplicationContext(), Arrays.asList(SCOPES))
                .setBackOff(new ExponentialBackOff());

 startActivityForResult(
                    mCredential.newChooseAccountIntent(),
                    REQUEST_ACCOUNT_PICKER);

 protected void onActivityResult(
            int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

          switch (requestCode) {

            case REQUEST_ACCOUNT_PICKER:
                if (resultCode == RESULT_OK && data != null &&
                        data.getExtras() != null) {
                    String accountName =
                            data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
                    if (accountName != null) {
                        mCredential.setSelectedAccountName(accountName);
                    }
                }
                break;
        }
}

在abovr代码中,我不想使用 mCredential.newChooseAccountIntent()我只想在没有此方法的情况下设置帐户,所以它可以。?

我在谷歌部分的Es文件资源管理器中看到这一点,不使用此方法登录谷歌驱动器。

1 个答案:

答案 0 :(得分:0)

如果您不想使用newChooseAccountIntent()

,可以使用此功能
 GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestEmail()
                    .requestIdToken(serverClientId)
                    .requestScopes(new Scope(DriveScopes.DRIVE_FILE))
                    .build();

            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
                    .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                    .build();

            mGoogleApiClient.connect();

            Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
            startActivityForResult(signInIntent, RC_AUTHORIZE_CONTACTS);

参考链接:https://developers.google.com/identity/sign-in/android/start-integrating