在Java中为GAE获取凭据的问题

时间:2017-01-11 09:47:00

标签: java google-app-engine

我正在尝试从Google App Engine上运行的Java servlet访问Google Drive API。我在本地工作但是在部署到Google App Engine时我不得不改变一些东西,例如使用不能在文件系统上工作的数据存储。我无法使用此功能,并且我根据Google的日历相关示例进行了此操作。我的代码如下所示:

private static  Credential authorize(HttpTransport HTTP_TRANSPORT, DataStoreFactory dataStoreFactory) throws IOException {
    // Load client secrets.
    InputStream in =
            ConcreteSheetWriter.class.getResourceAsStream(SECRET_PATH);
    GoogleClientSecrets clientSecrets =
            GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

    // Build flow and trigger user authorization request.
    GoogleAuthorizationCodeFlow flow =
            new GoogleAuthorizationCodeFlow.Builder(
                    HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                    .setDataStoreFactory(dataStoreFactory)
                    .setAccessType("offline")
                    .build();
   /*

   The credentials before deploying to GAE. Problems when deploying on GAE
   Credential credential = new AuthorizationCodeInstalledApp(
            flow, new LocalServerReceiver()).authorize("user");
    */
  final  String userId = UserServiceFactory.getUserService().getCurrentUser().getUserId();
  final  Credential credential = flow.loadCredential(userId);
  return credential;
}

当前的问题是我在将userId赋值给一行的行上得到一个空指针异常。在评论中,您可以看到我在GAE之前如何创建Credential实例。我甚至不确定" flow.loadCredential(...)"是正确的方法。如果这里的任何人对如何使这项工作有任何建议,那么我会非常感激。

0 个答案:

没有答案