【问题标题】:GoogleApiClient connection failed with ConnectionResult statuscode SIGN_IN_REQUIREDGoogleApiClient 连接失败,ConnectionResult 状态码为 SIGN_IN_REQUIRED
【发布时间】:2016-12-30 15:04:29
【问题描述】:

[编辑] - 赏金

我正在尝试将我的 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();
        }
    

还在寻找正确的实现方式……

【问题讨论】:

  • “由于没有传递帐户名称,因此提示用户选择” - 会发生这种情况吗?如果你得到那个屏幕,那么它就可以工作。您的错误是您需要登录有效帐户
  • @cricket_007,是的,我得到了选择帐户屏幕。一旦我选择了帐户,相同的屏幕就会一次又一次地循环显示。
  • @cricket_007,你能检查我编辑的代码,让我知道正确实现 GoogleApiClient 的最佳方法是什么?
  • 我自己从未做过,但如果不推荐使用 plus api,我会阅读有关替代方案的文档
  • 试试这个:从您的 gmail 帐户转到 google api 控制台。在这里,您将在 Google drive api 前面看到一个禁用按钮。就在你会看到一个齿轮或设置按钮点击它并生成oAuth令牌。

标签: android google-drive-android-api


【解决方案1】:

看来你的问题是因为

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

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

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

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

编码愉快! :)

【讨论】:

  • 将代码移动到 onStart() 和 onStop() 解决了这个问题。
【解决方案2】:

好吧,我在 github 上遇到了 Google drive 的问题,我发现他们通过 @iuribtt 回答遇到了类似的问题

这是链接

Google Drive Issues

如果此链接已过期,我将发布他的答案:

尝试使用“keytool -exportcert -alias androiddebugkey -keystore C:\Users\XXXXX.android\debug.keystore -list -v” 而不是您生成的密钥库,一旦您需要调试模式。

“debug.keystore”的路径和密码是“android” https://support.google.com/cloud/answer/6158849?hl=en#android

然后在https://developers.google.com/mobile/add中创建项目

最后,在https://console.developers.google.com中启用Drive API

希望这有帮助!!!干杯!!!

【讨论】:

  • 我认为您必须在控制台中设置生产和调试密钥。
猜你喜欢
  • 1970-01-01
  • 2015-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-01
  • 1970-01-01
  • 2018-10-08
相关资源
最近更新 更多