GoogleApiClient连接失败并显示ConnectionResult状态码SIGN_IN_REQUIRED

时间:2016-12-30 15:04:29

标签: android google-drive-android-api

[编辑] - 为赏金

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

这个问题已被多次询问,但他们都没有解决问题的方法。所以,我再次问这个问题。

我是第一次使用Google Drive Android API,而且无法通过"选择' APP NAME'""屏幕。以下是我现在所做的事情:

  1. 试图按照以下网址:
  2. 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. 使用compile' com.google.android.gms:play-services-drive:10.0.1'在我的build.gradle中(如果这有任何区别)

    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();
          }
      
    3. 仍在寻找正确的实施方式......

3 个答案:

答案 0 :(得分:3)

看起来你遇到的问题是因为

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

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

结果,您在选择帐户后(当调用onResume和onActivityResult时)有新的GoogleApiClient。

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

快乐的编码! :)

答案 1 :(得分:1)

好吧,我在github上遇到谷歌驱动器的问题,我发现类似的问题他们经过@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

中启用云端硬盘API

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

答案 2 :(得分:0)

您是否添加了GET_ACCOUNTS的权限?像这样的东西

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