2

在我们的 GAE 应用程序中,我们在谷歌驱动器中处理用户的文档,并且由于此过程有时需要超过 30 秒,因此我们会收到截止日期异常,因为它是 GAE 前端实例。

我们想使用后端实例。问题是如何通过凭据(com.google.api.client.auth.oauth2.Credentials)来初始化 Google 驱动 API。

如何将用户凭据传递到 GAE 任务队列,然后传递到后端实例以便稍后在任务运行时使用它们?

com.google.api.client.auth.oauth2.Credentials is not serializable ...

有什么办法吗?

4

2 回答 2

2

您需要保留凭证的输入,以便您可以在后端处理程序中重新创建它。

这可以是授权码,也可以是您为换取授权码而获得的访问令牌和刷新令牌。它们都是字符串,所以应该很容易序列化。

如果这一切听起来不熟悉,我很想知道您最初是如何获得证书的。以下有用文档的链接:

值得注意的是,最后一个链接中的示例代码特别包含一个您应该实现的方法来隐藏访问/刷新令牌:

/**
 * Retrieved stored credentials for the provided user ID.
 *
 * @param userId User's ID.
 * @return Stored Credential if found, {@code null} otherwise.
 */
static Credential getStoredCredentials(String userId) {
  // TODO: Implement this method to work with your database. Instantiate a new
  // Credential instance with stored accessToken and refreshToken.
  throw new UnsupportedOperationException();
}
于 2013-01-23T09:09:30.433 回答
0

我建议使用AppEngineCredentialStore来存储访问和刷新令牌。请查看calendar-appengine-sample作为示例用法。这是来自Utils.java的示例代码片段:

  static GoogleAuthorizationCodeFlow newFlow() throws IOException {
    return new GoogleAuthorizationCodeFlow.Builder(
        HTTP_TRANSPORT,
        JSON_FACTORY,
        getClientCredential(),
        Collections.singleton(CalendarScopes.CALENDAR))
        .setCredentialStore(new AppEngineCredentialStore())
        .setAccessType("offline")
        .build();
  }

注意:我是google-api-java-client项目的所有者。

于 2013-01-26T17:06:24.790 回答