4

我想我的應用程序連接到谷歌播放服務來訪問谷歌驅動器,但它顯示我連接用的StatusCode SIGN_IN_REQUIRED.GoogleApiClient onConnectionFailed用的StatusCode = SIGN_IN_REQUIRED

失敗但似乎會那樣,短短3天前。我也檢查了谷歌的開發者控制檯。他們可能已經改變了我無法弄清的API。您的幫助將受到歡迎。

我的代碼:

/** 
* Create a new file and save it to Drive. 
*/ 
    private void saveFileToDrive(final byte[] data) { 
    // Start by creating a new contents, and setting a callback. 
    Log.i(TAG, "Creating new contents."); 
    //final Bitmap image = mBitmapToSave; 
    Drive.DriveApi.newDriveContents(mGoogleApiClient) 
      .setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() { 

       @Override 
       public void onResult(DriveApi.DriveContentsResult result) { 
        // If the operation was not successful, we cannot do anything 
        // and must 
        // fail. 
        if (!result.getStatus().isSuccess()) { 
         Log.i(TAG, "Failed to create new contents."); 
         return; 
        } 
        // Otherwise, we can write our data to the new contents. 
        Log.i(TAG, "New contents created."); 
        // Get an output stream for the contents. 
        OutputStream outputStream = result.getDriveContents().getOutputStream(); 
        // Write the bitmap data from it. 
        /* ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream(); 
        image.compress(Bitmap.CompressFormat.PNG, 100, bitmapStream);*/ 
        try { 
         //outputStream.write(bitmapStream.toByteArray()); 
         outputStream.write(data); 
        } catch (IOException e1) { 
         Log.i(TAG, "Unable to write file contents."); 
        } 
        // Create the initial metadata - MIME type and title. 
        // Note that the user will be able to change the title later. 
        MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder() 
          .setMimeType("application/pdf").setTitle(filename).build(); 
        // Create an intent for the file chooser, and start it. 
        IntentSender intentSender = Drive.DriveApi 
          .newCreateFileActivityBuilder() 
          .setInitialMetadata(metadataChangeSet) 
          .setInitialDriveContents(result.getDriveContents()) 
          .build(mGoogleApiClient); 
        try { 
         startIntentSenderForResult(
           intentSender, REQUEST_CODE_CREATOR, null, 0, 0, 0); 
        } catch (IntentSender.SendIntentException e) { 
         Log.i(TAG, "Failed to launch file chooser."); 
        } 
       } 
      }); 
} 

@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. Once connected, the camera is launched. 
    mGoogleApiClient.connect(); 

} 



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


@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    switch (requestCode) { 
     case REQUEST_CODE_CREATOR: 
      // Called after a file is saved to Drive. 
      if (resultCode == RESULT_OK) { 
       Log.i(TAG, "Image successfully saved."); 

       Intent intent = new Intent(); 
       intent.putExtra("FILE_PATH",file_path); 
       PreviewActivity.this.setResult(RESULT_OK, intent); 
       finish(); 
       progressDialog.hide(); 

      } 
      break; 
    } 
} 

@Override 
public void onConnected(Bundle bundle) { 

    Log.i(TAG, "API client connected."); 
    //saveFileToDrive(); 
} 

@Override 
public void onConnectionSuspended(int i) { 
    Log.i(TAG, "GoogleApiClient connection suspended"); 
} 

@Override 
public void onConnectionFailed(ConnectionResult connectionResult) { 
    // Called whenever the API client fails to connect. 
    Log.i(TAG, "GoogleApiClient connection failed: " + connectionResult.toString()); 
    if (!connectionResult.hasResolution()) { 
     // show the localized error dialog. 
     GoogleApiAvailability.getInstance().getErrorDialog(this, connectionResult.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 { 
     connectionResult.startResolutionForResult(this, REQUEST_CODE_RESOLUTION); 
    } catch (IntentSender.SendIntentException e) { 
     Log.e(TAG, "Exception while starting resolution activity", e); 
    } 
} 

/END OF CODE/

搖籃:

apply plugin: 'com.android.application' 

android { 


dexOptions { 
    incremental true 
    javaMaxHeapSize "2048M" 

} 

compileSdkVersion 23 
buildToolsVersion '23.0.2' 
defaultConfig { 
    multiDexEnabled true 
    applicationId "com.woundcentrics.abxsteward" 
    minSdkVersion 14 
    targetSdkVersion 21 
    versionCode 1 
    versionName "0.4 (Beta)" 

} 
buildTypes { 
    release { 
     minifyEnabled false 
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    } 
} 
packagingOptions { 
    exclude 'META-INF/LICENSE.txt' 
    exclude 'META-INF/LICENSE' 
    exclude 'META-INF/NOTICE.txt' 
    exclude 'META-INF/NOTICE' 
    exclude 'META-INF/DEPENDENCIES' 
} 
productFlavors { 
} 
} 

dependencies { 
compile fileTree(dir: 'libs', include: ['*.jar']) 
testCompile 'org.robolectric:robolectric:3.0' 
compile project(':openCVLibrary310') 
compile files('libs/itextpdf-5.1.0.jar') 
compile project(':scanlibrary') 
apply plugin: 'com.google.gms.google-services' 
compile 'me.dm7.barcodescanner:zbar:1.8.3' 
compile 'com.mcxiaoke.volley:library-aar:1.0.0' 
compile 'com.google.code.gson:gson:2.4' 
compile 'com.android.support:appcompat-v7:23.0.0' 
compile 'com.google.android.gms:play-services:8.3.0' 
compile 'com.google.android.gms:play-services-ads:8.3.0' 
compile 'com.google.android.gms:play-services-identity:8.3.0' 
compile 'com.google.android.gms:play-services-gcm:8.3.0' 

} 
repositories { 
jcenter() 

} 

logcat的消息:

GoogleApiClient connection failed: ConnectionResult{statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent{21d08740: [email protected]}, message=null} 

回答

4

嗯,我得到了在logcat中顯示的消息:

GoogleApiClient onConnectionFailed用的StatusCode = SIGN_IN_REQUIRED

登錄所需的手段,我們需要啓用谷歌雲端硬盤API https://console.developers.google.com

enter image description here

下載client_id.json文件,並添加到我們的項目的/app文件夾:

enter image description here

然後我們就可以沒有問題訪問,讓我們期待API client connected.

enter image description here

爲了更好地利用的Google Drive API,建議使用此權限:

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> 

你可以在這裏找到一個完整的例子:

https://github.com/Jorgesys/Android-Google-Drive

enter image description here

+0

爲什麼這個'client_id.json'文件甚至在構建期間打包在哪裏? – mDroidd

+0

感謝這就是我一直在尋找 –

0

Here是您收到錯誤的信息。我假設你是,但你需要登錄才能訪問雲端硬盤。當我運行示例應用程序時,它最初要求我選擇一個帳戶。也許你沒有與你正在使用的設備同步的帳戶?有一個「添加賬戶」的選項,但是當你擁有零賬戶時,行爲可能會有所不同。文檔建議您或者不使用API​​(僅僅因爲您不能登錄才能繼續)或者調用startResolutionForResult(Activity,int)來提示用戶登錄,但只需添加account to your device就可以了。

+1

感謝您的快速回復。其實它早些時候正在工作。但它似乎就像3天前那樣。我也檢查了谷歌的開發者控制檯。他們可能已經改變了我無法弄清的API。您的幫助將受到歡迎。再次感謝。 –

+0

我只想添加一些指示,以跟進@克里希納的答案。您只需將處理程序添加到Google Drive API的連接和授權中即可。這可以通過'GoogleApiClient'完成。這個可以在OnCreate()方法中找到。您可以嘗試閱讀[連接授權]的實施指南(https://developers.google.com/drive/android/auth#connecting_and_authorizing_the_google_drive_android_api)以獲得澄清。這可能會解決您的問題,因爲GoogleApiClient連接失敗。我希望這有幫助。 :) –

+0

@AbdulYasin Iam面臨同樣的問題。我的應用程序工作正常,直到今天,但現在IAM無法登錄。選擇帳戶繼續詢問,它是返回谷歌API客戶端失敗結果代碼4.我沒有改變任何東西。感謝提前:) –

0

此錯誤表明該帳戶被鎖定,可能出於安全原因。您應該從桌面瀏覽器訪問drive.google.com,然後登錄該帳戶,然後嘗試通過Android應用程序以編程方式訪問該帳戶。完成桌面瀏覽器登錄後,您應該可以從Android訪問該帳戶。

+0

帳戶在移動設備和網絡瀏覽器上都正常工作。 –

0

這將在創建您的Google API項目後生效,您已按問題添加了SHA-1和包名稱。 另外,請記住,您的包不得與Google中的其他項目發生衝突。 希望這個作品。 註冊您的項目中:

Click here to add enable your Google api

,並啓用API一旦項目證書都是集。 如果你正在尋找上載或下載你的文件,那麼你必須在你的清單文件使用權限:

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

-Surajit。 快樂編碼。

0

當我看到大多數的答案以上是正確的,還是如果你能不能夠解決問題。這裏有事情,請與您的開發環境

  1. 檢查您是否已經兩個不同的系統(PC /筆記本電腦)之間切換,請產生另一SHA1密鑰每個系統和更新谷歌開發者控制檯

  2. 如果您不是兩個系統之間的切換還是你所面對的問題,那麼,請確保您清單應用程序ID包名稱匹配你在谷歌開發者CONSOL給Ë

  3. 不要忘記使驅動器API在谷歌開發者控制檯

相關問題