【问题标题】:Google service account authorization errorGoogle 服务帐号授权错误
【发布时间】:2020-04-07 20:07:57
【问题描述】:

我正在尝试通过服务帐户使用 Google API。 最初,我使用带有凭据的普通项目帐户,并设法查询了 API。为了使用服务帐户,我修改了代码以跳过浏览器身份验证流程并仅使用服务帐户凭据。似乎与正常项目凭据一起使用的相同请求现在给出了一个错误,而没有在消息中澄清太多。如下:

directoryService = new GoogleService().buildDirectoryService();

Users result = directoryService.users().list()
        .setCustomer("my_customer")
        .setOrderBy("email")
        .execute();

public Directory buildDirectoryService() throws GeneralSecurityException, IOException {
        final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();

        GoogleCredential credential = GoogleCredential
                .fromStream(new FileInputStream("src/main/resources/newest_service_account_creds.json"))
                .createScoped(SCOPES);

        return new Directory.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
                .setApplicationName(APPLICATION_NAME)
                .setHttpRequestInitializer(credential).build();
    }

不幸的是,错误信息并没有说太多:

Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
{
  "code" : 400,
  "errors" : [ {
    "domain" : "global",
    "message" : "Invalid Input",
    "reason" : "invalid"
  } ],
  "message" : "Invalid Input"
}
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:146)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1108)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
at GetDrivePermissions.getFiles(GetDrivePermissions.java:35)
at Main.main(Main.java:7)

我重复了该过程两次以确保没有遗漏任何内容,包括创建新项目、启用 API、添加服务帐户、添加和启用域范围委派以及获取凭据 json 文件,还添加在管理 UI 的应用访问控制中访问应用。但是调用

directoryService.users().list().setCustomer("my_customer").setOrderBy("email").execute();

服务仍然返回上述错误信息。

错误消息是指“无效输入”,但该请求以前可以正常工作,并且具有正常的应用程序凭据。所以问题一定不在于我如何调用目录服务,而在于我的身份验证,这是工作版本和非工作版本之间的唯一区别。

由于这与使用项目凭据一起工作,我使用服务帐户的方式一定有问题,但即使重复整个过程两次,我也找不到错误。根据我找到的有关服务帐户的指南,这应该可以工作。凭据对象已创建并且看起来正确。

接下来我可以研究什么?

感谢您提前提供任何见解。

【问题讨论】:

    标签: java google-api service-accounts


    【解决方案1】:

    我将此部分改回以使用应用程序自己的凭据。我有另一个使用服务帐户凭据的调用并且可以正常工作,因此这是目前的解决方法。

    【讨论】:

    • 嗨@RekaK。我处于完全相同的情况,使用Java。所以,你所做的基本上是停止使用服务帐户,转而使用一些标准的 API 密钥或 Oauth,对吧?我不想那样做,因为我的应用程序在 docker 上运行,每次部署新版本时通过浏览器手动授权它真的很痛苦——这几乎每天都会发生。令我惊讶的是,官方文档如此广泛且不精确。有很多人抱怨它。
    • 嗨,@NicoJones。是的,不幸的是,这就是我目前所能做的。
    猜你喜欢
    • 2017-06-13
    • 1970-01-01
    • 1970-01-01
    • 2014-07-19
    • 2013-08-21
    • 2012-05-20
    • 1970-01-01
    • 1970-01-01
    • 2012-11-24
    相关资源
    最近更新 更多