gpt4 book ai didi

android - GoogleApiClient 连接失败,ConnectionResult 状态码为 SIGN_IN_REQUIRED

转载 作者:行者123 更新时间:2023-11-29 15:40:23 24 4
gpt4 key购买 nike

[编辑] - 赏金

我正在尝试将我的 Android 应用程序与 Google Drive 连接以同步 MP3 音频文件。需要使用 GoogleDriveAndroidApi 和 GoogleApiClient 连接 Google Drive 的工作代码/示例(除了下面给出的链接)。


这个问题已经被问过很多次了,但是没有一个解决这个问题。所以,我再次提出这个问题。

我是第一次使用 Google Drive Android API,无法通过“为‘APP NAME’选择帐户”屏幕。这是我现在所做的:

  1. 尝试访问以下网址:

https://developers.google.com/drive/android/get-started

https://github.com/googledrive/android-quickstart

https://www.numetriclabz.com/integrate-google-drive-in-android-tutorial/

  1. 在我的 build.gradle 中使用 compile 'com.google.android.gms:play-services-drive:10.0.1'(如果有任何不同)

  2. 到目前为止尝试的代码:

     public class ARCBackupActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener {

    private static final int REQUEST_CODE = 1;

    private GoogleApiClient mGoogleApiClient;
    private Toolbar mToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.arc_activity_backup);

    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);

    getSupportActionBar().setDisplayShowTitleEnabled(false);
    getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    }


    @Override
    protected void onResume() {
    super.onResume();
    if (mGoogleApiClient == null) {
    // Create the API client and bind it to an instance variable.
    // We use this instance as the callback for connection and connection
    // failures.
    // Since no account name is passed, the user is prompted to choose.
    mGoogleApiClient = new GoogleApiClient.Builder(this)
    .addApi(Drive.API)
    .addScope(Drive.SCOPE_FILE)
    .addConnectionCallbacks(this)
    .addOnConnectionFailedListener(this)
    .build();
    }
    // Connect the client.
    mGoogleApiClient.connect();
    }

    @Override
    protected void onPause() {
    if (mGoogleApiClient != null) {
    mGoogleApiClient.disconnect();
    }
    super.onPause();
    }

    @Override
    protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    switch (requestCode) {

    case REQUEST_CODE:

    if (resultCode == Activity.RESULT_OK) {
    mGoogleApiClient.connect();
    }
    break;
    }
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
    // Called whenever the API client fails to connect.
    Log.i(Const.DEBUG, "GoogleApiClient connection failed: " + result.toString());
    if (!result.hasResolution()) {
    // show the localized error dialog.
    GoogleApiAvailability.getInstance().getErrorDialog(this, result.getErrorCode(), 0).show();
    return;
    }
    // The failure has a resolution. Resolve it.
    // Called typically when the app is not yet authorized, and an
    // authorization
    // dialog is displayed to the user.
    try {
    result.startResolutionForResult(this, REQUEST_CODE);
    } catch (IntentSender.SendIntentException e) {
    Log.e(Const.DEBUG, "Exception while starting resolution activity", e);
    }
    }

    @Override
    public void onConnected(Bundle connectionHint) {
    Log.d(Const.DEBUG, "API client connected.");

    //saveFileToDrive();
    }

    @Override
    public void onConnectionSuspended(int cause) {
    Log.d(Const.DEBUG, "GoogleApiClient connection suspended");
    }
    }

    我在这里缺少什么?非常感谢任何具有良好解释的完整示例。

    如果我需要提供更多信息来解决问题,请告诉我

    [编辑 1]

    如果我在构建 googleApiClient 时添加 Plus.API,它会解决卡在选择帐户循环中的问题。但是,Plus.API 已被弃用,是否有仅通过使用 GoogleApiClient 的解决方法?

    这里是修改后的代码:

     @Override
    protected void onResume() {
    super.onResume();

    if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
    Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
    mGoogleApiClient.disconnect();
    }


    if (mGoogleApiClient == null) {
    // Create the API client and bind it to an instance variable.
    // We use this instance as the callback for connection and connection
    // failures.
    // Since no account name is passed, the user is prompted to choose.
    mGoogleApiClient = new GoogleApiClient.Builder(this)
    .addApi(Drive.API)
    .addApi(Plus.API)
    .addScope(Drive.SCOPE_FILE)
    .addConnectionCallbacks(this)
    .addOnConnectionFailedListener(this)
    .build();
    }
    // Connect the client.
    mGoogleApiClient.connect();
    }

仍在寻找正确的实现方式...

最佳答案

看来你的问题是因为

@Override
protected void onPause() {
if (mGoogleApiClient != null) {
mGoogleApiClient.disconnect();
}
super.onPause();
}

让我解释一下。当帐户选择器出现时,它使您的 Activity 调用 onPause()。在 onPause() 中调用 mGoogleApiClient.disconnect()

因此,在选择帐户后(调用 onResume 和 onActivityResult 时)您将拥有新的 GoogleApiClient。

要解决此问题,只需将 mGoogleApiClient.connect()mGoogleApiClient.disconnect() 放在您 Activity 的 onStart()/onStop() 方法中或使用 enableAutoManage(Activity, OnConnectionFailedListener) 为您的 GoogleApiClient。

编码愉快! :)

关于android - GoogleApiClient 连接失败,ConnectionResult 状态码为 SIGN_IN_REQUIRED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41398312/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com