我在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));
}
}
}
是否要求用戶選擇一個帳戶的對話框?如果手機上有多個Google帳戶,並且您沒有使用'useDefaultAccount()'構建GoogleApiClient,則會顯示一個對話框,要求用戶選擇一個帳戶。你說對話「什麼都不做」。你能提供更多細節嗎? –
是的,是要求用戶選擇一個帳戶的對話框。當用戶選擇一個帳戶時,沒有任何反應。 我沒有使用useDefaultAccount選項構建我的客戶端。 –
實際上,即使用戶只有一個帳戶,也會顯示此對話框。 –