2015-06-29 103 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)讨论了其他人的困难已经有了这个。 –