【问题标题】:GoogleAccountCredential + Google Play Services + Refresh Token - how this works together?GoogleAccountCredential + Google Play Services + Refresh Token - 这是如何协同工作的?
【发布时间】:2013-02-19 15:40:12
【问题描述】:

大约 2 周前,我发布了一个非常成功的应用程序。但从昨天开始,用户不断向我发送有关 Drive 不再可用的电子邮件。经过快速调试,我发现对 Drive API 的请求现在返回“403 Forbidden”->“Access Not Configured”。

我认为这可能是没有正确处理刷新令牌的问题。

我正在使用以下代码(来自 Android Drive SDK 示例):

mCredentials = GoogleAccountCredential.usingOAuth2(this, DriveScopes.DRIVE);
String accountName = PreferenceManager.getDefaultSharedPreferences(this).getString(PREF_DRIVE_NAME, null);
if (accountName != null) {
     setupDrive(accountName);
} else {
     startActivityForResult(mCredentials.newChooseAccountIntent(), 0);
}

setupDrive(...) 看起来像这样:

 mCredentials.setSelectedAccountName(accountName);
 try {
        mCredentials.getToken();
 } catch (Exception e) {
        Log.w(AbstractDriveActivity.class.getSimpleName(), "Error getting auth token", e);
        if (e instanceof UserRecoverableAuthException) {
              Intent intent = ((UserRecoverableAuthException) e).getIntent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).addFlags(Intent.FLAG_FROM_BACKGROUND);
        startActivity(intent);
        } else {
               Toast.makeText(AbstractDriveActivity.this, getString(R.string.toast_drive_setup_error),
                            Toast.LENGTH_SHORT).show();
               finish();
        }
  }
  drive = new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(),
                    mCredentials).build();

知道这里可能出了什么问题吗?

据我了解,GoogleAccountCredential 使用 Google Play 服务来管理 OAuth2 流程,您只需提供用户名即可。我错了吗?我错过了什么吗?

顺便说一句:清除应用数据后,再次选择 Google 帐户,一切正常。这就是为什么我认为它与刷新令牌有关。

上帝

【问题讨论】:

    标签: android google-drive-api google-play-services


    【解决方案1】:

    没有保证,但问题可能来自这里:

    mCredentials = GoogleAccountCredential.usingOAuth2(this, DriveScopes.DRIVE);
    

    此方法对我来说似乎已弃用。您应该升级您的 SDK 和环境并将其更改为:

    mCredentials = GoogleAccountCredential.usingOAuth2(this, Arrays.asList(DriveScopes.DRIVE));
    

    【讨论】:

      猜你喜欢
      • 2022-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-26
      • 1970-01-01
      • 2016-11-21
      • 2019-06-16
      • 2016-08-30
      相关资源
      最近更新 更多