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」中所述的步驟執行「自動簽名」。顯然,使用與註冊應用程序時使用的密鑰庫相同的密鑰庫(以上)。

相關問題