4

我试图将Google Drive整合到我的应用程序中,但收效甚微。使用快速启动库中的代码,我收到以下错误消息每次我在选择的用户帐户登录时间:GoogleApiClient连接失败:ConnectionResult {statusCode = SIGN_IN_REQUIRED,

I/DriveDemo: GoogleApiClient connection failed: ConnectionResult{statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent{867e070: [email protected]}, message=null} 

我已按照以下网址的指示:

  1. https://developers.google.com/drive/android/get-started
  2. https://github.com/googledrive/android-quickstart

,随后在视频:

  1. https://youtu.be/RezC1XP6jcs

我也看了无数的堆栈溢出的问题和答案有不知道我做错了。

的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"); 
} 
+0

您是否在Google API控制台中注册开发/发布密钥? –

+0

遵循所有创建和注册密钥的步骤。 – lightkeeper

+0

@lightkeeper在开发人员控制台中使用包和SHA键在开发人员控制台中创建AuthKey,并且您可以很好地去... – Kushan2

回答

0

根据相关文档 - SIGN_IN_REQUIRED

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

您还可以检查@卓的问题,一个SO post是建议适当加你的SHA-1和包名称和要求启用API一旦项目证书都是集。

希望这些信息对您有所帮助。

+0

我遵循所有步骤来创建密钥并将其注册到API控制台中。 API在控制台中显示为活动状态。这就是我被卡住的原因。 – lightkeeper

0

我后,我保证每个APK建立由我做“找一个Android证书和注册应用程序”中的说明here

当使用Studio时使用的相同密钥库签署了得到固定了类似的问题,我按照here中所述的“配置构建过程以自动签名APK”中所述的步骤执行“自动签名”。显然,使用与注册应用程序时使用的密钥库相同的密钥库(以上)。

相关问题