谷歌服务账号授权错误

huangapple 未分类评论46阅读模式
英文:

Google service account authorization error

问题

我试图使用Google APIs与服务账号。最初我使用了一个普通的项目账号和凭证,成功地查询了APIs。为了使用服务账号,我修改了代码以跳过浏览器认证流程,只使用服务账号凭证。然而,之前使用普通项目凭证可以正常工作的相同请求,现在却在没有明确说明太多内容的情况下出错了。错误如下:

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)

我多次重复了这个过程,以确保没有遗漏任何步骤,包括创建新项目,启用APIs,添加服务账号,添加并启用域广泛委派以及获取凭证JSON文件,还在管理员界面的应用访问控制中添加了访问权限。但是调用以下代码仍然返回上述错误信息:

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

错误信息提到了“无效输入”,但之前使用普通应用凭证的请求是正常工作的。因此,问题不可能出现在我如何调用Directory服务上,而可能出现在我的认证方式上,这是工作版本和非工作版本之间唯一的区别。

由于在使用项目凭证时这个方法是可行的,所以在我使用服务账号时肯定出现了问题,但即使我重复整个过程两次,也无法找到错误所在。根据我找到的关于服务账号的指南,这个应该是可行的。凭证对象已被创建并且看起来是正确的。

接下来我可以查看什么?提前感谢您提供的任何见解。

英文:

I'm trying to use Google APIs with a service account.
Originally I used a normal project account with credentials and I managed to query the APIs. To use a service account, I modified the code to skip the browser auth flow and only use the service account credentials. It seems that the same request that used to work with the normal project credentials now give an error without clarifying much in the message. It goes as follows:

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

Unfortunately the error message is not saying much:

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)

I repeated the process two times to make sure I didn't miss anything, including creating a new project, enabling the APIs, adding a service account, adding and enabling domain-wide delegation and obtaining a credentials json file, also adding access to the app in App Access Control in the admin UI. But calling

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

service still returns the above error message.

The error message refers to "Invalid input", but the request used to work before, with normal application credentials. So the problem must lay not in how I call the Directory service but with my authentication, the only difference between the working and the non-working version.

Since this worked with using the project credentials, there must be something wrong with how I use the service account, but I can't find the error even after repeating the whole process two times. Based on the guides I found about service accounts, this is supposed to work. The credential object is created and it looks proper.

What can I look into next?

Thnaks for any insight in advance.

答案1

得分: 0

我将此部分更改回使用应用程序自己的凭据。我还有另一个调用使用服务帐户凭据,而且那个是有效的,所以目前这只是一个解决方法。

英文:

I changed this part back to use the application's own credentials. I have another call that uses the service account credentials and that works, so this is a workaround for now.

huangapple
  • 本文由 发表于 2020年4月8日 04:07:57
  • 转载请务必保留本文链接:https://java.coder-hub.com/61088561.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定