2015-06-29 102 views
1

我在Android應用中遇到了Google Drive API問題。在我的設備上,使用Android 5.1.1的Nexus 4工作正常。在我用戶的設備上,我沒有顯示我配置的同意屏幕,相反,用戶的電子郵件只顯示一個列表選擇對話框,但它沒有任何作用。Android Google Drive API身份驗證

我在Android 5.0.1的其他設備上看到了這個問題。

我使用我的生產SHA1密鑰和同意屏幕配置了OAuth 2.0憑據。就像我說的,它在我的設備上工作正常。

我用下面的參數來編譯我的應用程序:

  • 的minSdkVersion = 15
  • targeSdKVerion = 22
  • compileSdkVersion = 22
  • 生成工具:22.0.1
  • 平臺工具:23rc3

而這些谷歌圖書館:

  • com.android.support:support-v4:22.2.0
  • com.android.support:appcompat-v7:22.2.0
  • com.android.support:design:22.2.0
  • com.google.android.gms:播放服務的廣告:7.5.0
  • com.google.android.gms:播放服務驅動:7.5.0

謝謝!

這裏是我的代碼現在的樣子:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == REQUEST_CODE_RESOLUTION) { 
     if (resultCode == RESULT_OK) { 
      // Make sure the app is not already connected or attempting to connect. 
      if (!mBackupAsyncTask.getGoogleApiClient().isConnecting() && 
        !mBackupAsyncTask.getGoogleApiClient().isConnected()) { 
       // Connect and execute the task. 
       mBackupAsyncTask.getGoogleApiClient().connect(); 
      } 
     } 
    } 
} 

@Override 
public void onConnectionFailed(ConnectionResult connectionResult) { 
    Log.i(LOG_TAG, "GoogleApiClient connection failed: " + connectionResult.toString()); 

    if (!connectionResult.hasResolution()) { 
     Log.i(LOG_TAG, "Error with no resolution."); 
     // Show the localized error dialog. 
     GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(), this, 0).show(); 
    } else { 
     // 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(LOG_TAG, "Exception while starting resolution activity.", e); 
     } 
    } 
} 

public class DriveBackupAsyncTask extends ApiClientAsyncTask<Object, Void, Metadata> { 

    public DriveBackupAsyncTask(Context context) { 
     super(context); 
    } 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     mProgressBar.setIndeterminate(true); 
     mEfetuarBackupButton.setEnabled(false); 
     mRestaurarBackupButton.setEnabled(false); 
    } 

    @Override 
    protected Metadata doInBackgroundConnected(Object... params) { 
     if (params[0].equals(TASK_BACKUP)) { 
      DriveFile backupFile = DriveBackupHelper.createBackup(
        getGoogleApiClient(), 
        APP_FOLDER, 
        BACKUP_FILE, 
        getDbFile()); 

      if (backupFile != null) 
       return backupFile.getMetadata(getGoogleApiClient()).await().getMetadata(); 
     } 

     if (params[0].equals(TASK_RESTAURAR)) { 
      DriveFile backupFile = DriveBackupHelper.getBackupFile(
        getGoogleApiClient(), 
        APP_FOLDER, 
        BACKUP_FILE); 

      if (backupFile == null) 
       return null; 

      if (DriveBackupHelper.restoreBackup(getGoogleApiClient(), getDbFile(), backupFile)) 
       return backupFile.getMetadata(getGoogleApiClient()).await().getMetadata(); 
     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(Metadata result) { 
     super.onPostExecute(result); 
     mProgressBar.setIndeterminate(false); 
     mEfetuarBackupButton.setEnabled(true); 
     mRestaurarBackupButton.setEnabled(true); 

     if (result == null) { 
      showError(getString(R.string.bkp_error)); 
     } else { 
      showMessage(getString(R.string.bkp_success)); 
     } 
    } 
} 
+0

是否要求用戶選擇一個帳戶的對話框?如果手機上有多個Google帳戶,並且您沒有使用'useDefaultAccount()'構建GoogleApiClient,則會顯示一個對話框,要求用戶選擇一個帳戶。你說對話「什麼都不做」。你能提供更多細節嗎? –

+0

是的,是要求用戶選擇一個帳戶的對話框。當用戶選擇一個帳戶時,沒有任何反應。 我沒有使用useDefaultAccount選項構建我的客戶端。 –

+1

實際上,即使用戶只有一個帳戶,也會顯示此對話框。 –

回答

1

這說明假設你的代碼是基於Google API Guide處理連接故障和結構類似於下面的例子(這是從指南複製):

@Override 
public void onConnectionFailed(ConnectionResult result) { 
    if (mResolvingError) { 
     // Already attempting to resolve an error. 
     return; 
    } else if (result.hasResolution()) { 
     try { 
      mResolvingError = true; 
      // This statement causing posting of the account picker dialog 
      // or other UI to resolve the connection problem. 
      result.startResolutionForResult(this, REQUEST_RESOLVE_ERROR); 
     } catch (SendIntentException e) { 
      // There was an error with the resolution intent. Try again. 
      mGoogleApiClient.connect(); 
     } 
    } else { 
     // Show dialog using GooglePlayServicesUtil.getErrorDialog() 
     showErrorDialog(result.getErrorCode()); 
     mResolvingError = true; 
    } 
} 

您看到的對話框是在用戶需要選擇帳戶時創建的。它是由該聲明onConnectionFailed()產生:

result.startResolutionForResult(this, REQUEST_RESOLVE_ERROR); 

REQUEST_RESOLVE_ERROR是一個整型常量定義。當用戶在對話框中選擇一個帳戶時,您的活動將收到請求代碼等於REQUEST_RESOLVE_ERROR的結果。

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == REQUEST_RESOLVE_ERROR) { 
     mResolvingError = false; 
     if (resultCode == RESULT_OK) { 
      // Make sure the app is not already connected or attempting to connect 
      if (!mGoogleApiClient.isConnecting() && 
        !mGoogleApiClient.isConnected()) { 
       mGoogleApiClient.connect(); 
      } 
     } 
    } 
} 

當您在您的文章說,對話「什麼都不做」,這表明你做的:您的活動必須能夠處理類似這樣的結果(也從指南複製)的方法沒有onActivityResult()方法,或者,如果這樣做,代碼沒有正確處理對話框的結果。

如果此答案對您沒有幫助,且您已有onActivityResult(),請將其與onConnectionFailed()一起發佈。

+0

好吧,我的壞。 起初我使用異步請求來訪問谷歌驅動器,但後來我改變了使用這個演示應用程序同步請求:https://github.com/googledrive/android-demos/blob/master/src/com/google/我忘了檢查BaseDemoActivity,我從應用程序中剔除了onActivityResult,因爲我沒有看到演示中的方法,但忘記了檢查BaseDemoActivity。 現在我的應用程序似乎工作正常,我只是不知道爲什麼它在我的設備上工作。 –

+0

我還在學習GoogleApi連接。我的經驗是,第一次應用程序嘗試連接時,會顯示[AccountPicker](https://developers.google.com/android/reference/com/google/android/gms/common/AccountPicker),然後顯示同意對話。如果用戶完成了這兩項操作並建立了連接,則帳戶經理會將選定的帳戶保存爲默認帳戶以及任何OAuth令牌。在隨後的連接請求中,爲了方便用戶使用保存的值(在下一條評論中繼續)。 –

+0

對於開發測試,最好能夠清除保存的值,以執行連接失敗解決的所有代碼。我還沒有找到辦法做到這一點。這[相關問題](http://stackoverflow.com/questions/26457118/user-sign-out-clearing-the-default-google-account-does-not-cause-the-account-pi)討論了其他人的困難已經有了這個。 –