GoogleApiClient连接失败:ConnectionResult {statusCode = SIGN_IN_REQUIRED,

时间:2017-02-11 19:26:17

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

我正在尝试将Google云端硬盘集成到我的应用程序中,但收效甚微。使用快速启动存储库中的代码,每次选择登录用户帐户时都会收到以下错误消息:

I/DriveDemo: GoogleApiClient connection failed: ConnectionResult{statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent{867e070: android.os.BinderProxy@13cdfe9}, message=null}

我按照以下网址的说明进行操作:

  1. https://developers.google.com/drive/android/get-started
  2. https://github.com/googledrive/android-quickstart
  3. 并按照视频点击:

    1. https://youtu.be/RezC1XP6jcs
    2. 我还看了很多Stack Overflow问题和答案,不知道我做错了什么。

      的manifest.xml:

      <?xml version="1.0" encoding="utf-8"?>
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.lightkeeper54.chuck.drivedemo">
      
          <uses-permission android:name="android.permission.GET_ACCOUNTS" />
          <application
              android:allowBackup="true"
              android:icon="@mipmap/ic_launcher"
              android:label="@string/app_name"
              android:supportsRtl="true"
              android:theme="@style/AppTheme">
              <activity android:name=".MainActivity">
                  <intent-filter>
                      <action android:name="android.intent.action.MAIN" />
                      <category android:name="android.intent.category.LAUNCHER" />
                  </intent-filter>
              </activity>
          </application>
      
      </manifest>
      

      代码:

      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. Once connected, the camera is launched.
          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_CAPTURE_IMAGE:
                  // Called after a photo has been taken.
                  if (resultCode == Activity.RESULT_OK) {
                      // Store the image data as a bitmap for writing later.
                      mBitmapToSave = (Bitmap) data.getExtras().get("data");
                  }
                  break;
              case REQUEST_CODE_CREATOR:
                  // Called after a file is saved to Drive.
                  if (resultCode == RESULT_OK) {
                      Log.i(TAG, "Image successfully saved.");
                      mBitmapToSave = null;
                      // Just start the camera again for another photo.
                      startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE),
                              REQUEST_CODE_CAPTURE_IMAGE);
                  }
                  break;
          }
      }
      
      @Override
      public void onConnectionFailed(ConnectionResult result) {
          // Called whenever the API client fails to connect.
          Log.i(TAG, "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_RESOLUTION);
          } catch (IntentSender.SendIntentException e) {
              Log.e(TAG, "Exception while starting resolution activity", e);
          }
      }
      
      @Override
      public void onConnected(Bundle connectionHint) {
          Log.i(TAG, "API client connected.");
          if (mBitmapToSave == null) {
              // This activity has no UI of its own. Just start the camera.
              startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE),
                      REQUEST_CODE_CAPTURE_IMAGE);
              return;
          }
          saveFileToDrive();
      }
      
      @Override
      public void onConnectionSuspended(int cause) {
          Log.i(TAG, "GoogleApiClient connection suspended");
      }
      

2 个答案:

答案 0 :(得分:0)

根据文档 - SIGN_IN_REQUIRED

  

客户端尝试连接到该服务但用户未登录。客户端可以选择继续而不使用API​​。或者,如果hasResolution()返回true,则客户可以致电startResolutionForResult(Activity, int)以提示用户登录。在登录活动返回RESULT_OK后,进一步的尝试应该会成功。

您也可以查看@ Cheok的问题,SO post建议按要求正确添加SHA-1和包名称,并在项目凭据全部设置后启用API。

希望这些信息有所帮助。

答案 1 :(得分:0)

我遇到了类似的问题,我确保每个APK版本都是由我在执行时使用的同一个密钥库签署的,并且#34;获取Android证书并注册您的应用程序&#34;正如所描述的here

使用Studio时,我执行了#34;自动签名&#34;按照&#34;配置构建过程以自动签署APK&#34;中所述的步骤正如here所述。显然,使用与注册应用程序时使用的密钥库相同的密钥库(上图)。