【问题标题】:How to get a Google API credentials for testing the Drive API in a Java EE Web Application?如何获取用于在 Java EE Web 应用程序中测试 Drive API 的 Google API 凭据?
【发布时间】:2019-06-20 16:57:24
【问题描述】:

我正在使用 Java 企业版开发一个网络应用程序,我需要 Google Drive API。在某些时候,我需要让客户“授权”访问他们的云端硬盘,为此(根据我的理解),我需要获取 Google API 凭据(oAuth 的),但这是一个漫长的过程,因为我我在做这个学校项目,我等不及了。所以我需要一种方法来测试 API,而不必经历获取已发布应用所需凭据的繁重过程。

我正在这个项目上测试 Drive API https://github.com/google/google-api-java-client-samples/tree/master/drive-cmdline-sample

问题出在这个函数上

private static Credential authorize() throws Exception {
    // load client secrets
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
        new InputStreamReader(testApi.class.getResourceAsStream("/client_secrets.json")));
    if (clientSecrets.getDetails().getClientId().startsWith("Enter")
        || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
      System.out.println(
          "Enter Client ID and Secret from https://code.google.com/apis/console/?api=drive "
          + "into drive-cmdline-sample/src/main/resources/client_secrets.json");
      System.exit(1);
    }

我需要以某种方式获得client_secrets.json,但我不能没有凭据,或者至少,这是我从研究和阅读中了解到的。

如果有帮助,这个函数是从com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; 导入的。

谢谢!

【问题讨论】:

    标签: jakarta-ee google-drive-api credentials


    【解决方案1】:

    是的,您是对的,您需要进行身份验证才能使用 Google Drive API。否则,服务无法判断谁在访问/请求数据以及此人是否有权访问。

    不用担心,您不必等待,但您必须执行某些步骤才能获得此证书。我了解您希望授予其他客户访问权限,以便使用此 API 获取他们的信息。

    为此,您首先需要一个启用了 Drive API 的 GCP 项目。 https://developers.google.com/drive/api/v3/quickstart/java

    点击“启用 Drive API”会自动创建一个项目,很可能在启用 Drive API 的情况下称为 quickstart。

    然后您必须设置一个服务帐户才能访问此项目。 https://developers.google.com/identity/protocols/OAuth2ServiceAccount

    完成此操作后,将下载的文件是您进行身份验证所需的凭据。 (“client_secret.json”)

    【讨论】:

    • 感谢您的回答。我不知道我是否做对了,但我按照你的指示下载了两个文件(不确定哪个应该是“client_secret.json”,我一直在试图找到放在哪里它,我找不到正确的目录......你能帮我解决这个问题,以便我可以验证我的工作吗?
    • 单击“启用 Drive API”时创建的第一个文件,仅当您想将其用作 Web API 请求时才需要。但是,对于您想要实现的目标,您必须使用服务帐户的凭据 developers.google.com/identity/protocols/OAuth2ServiceAccount 文件的名称(取决于服务帐户的名称)-IDnumbers.json。注意:请不要将您的服务帐户的 json 文件分享给任何人,这样您就可以访问您的信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-24
    相关资源
    最近更新 更多