【问题标题】:GAE backend / task queue - how to pass user credentials in parameter?GAE 后端/任务队列 - 如何在参数中传递用户凭据?
【发布时间】:2013-01-20 22:31:10
【问题描述】:

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

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

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

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

有什么办法吗?

【问题讨论】:

    标签: java google-app-engine serialization google-drive-api google-api-java-client


    【解决方案1】:

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

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

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

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

    /**
     * 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();
    }
    

    【讨论】:

      【解决方案2】:

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

        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 项目的所有者。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-23
        • 1970-01-01
        • 1970-01-01
        • 2012-07-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多