线程“主”中的com.google.api.client.googleapis.json.GoogleJsonResponseException异常:503服务不可用

时间:2019-12-09 08:21:54

标签: java google-api google-oauth google-directory-api admin-sdk

我正在尝试使用服务帐户列出域中的用户。

 String userEmail = "name@yourdomain.com";

GoogleCredential credential = new GoogleCredential.Builder()
                .setTransport(httpTransport)
                .setJsonFactory(JSON_FACTORY)
                .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
                .setServiceAccountPrivateKey(k.getPrivate())
                .setServiceAccountScopes(SCOPES)
            //    .setServiceAccountUser(userEmail)
                .build();   

//Call directory API
Directory service = new Directory.Builder(httpTransport, JSON_FACTORY, credential)
          .setApplicationName(APPLICATION_NAME)
          .build();

        // List the users in your domain
        Users result = service.users().list()
               .setCustomer("my_customer")
               .setMaxResults(10)
               .setOrderBy("email")
               .execute();
       List<User> users = result.getUsers();
       if (users == null || users.size() == 0) {
           System.out.println("No users found.");
       } else {
           System.out.println("Users:");
           for (User user1 : users) {
               System.out.println(user1.getName().getFullName());
           }
       }

我已经通过遵循Quickstart使用Oauth客户端ID来完成此操作。但是我不想向用户显示授权屏幕。因此,我使用服务帐户来实现域范围的委派。

因此,我已经从documentation中获取了代码来使用服务帐户。我使用密钥库从.p12文件中获得了私钥。我遵循了这个answer(@ sdm的答案)。当我运行下面的程序时,它给了我下面的错误。

Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 503 Service Unavailable
{
  "code" : 503,
  "errors" : [ {
    "domain" : "global",
    "message" : "Service unavailable. Please try again",
    "reason" : "backendError"
  } ],
  "message" : "Service unavailable. Please try again"
}
    at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:150)
    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:1067)
    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 com.satheesh.classroomServiceAccount.ClassroomServiceAccount.main(ClassroomServiceAccount.java:125)

当我取消注释.setServiceAccountUser(userEmail)时,它会给我

  

线程“ main”中的异常   com.google.api.client.auth.oauth2.TokenResponseException:401   未经授权

我给了用户正确的电子邮件ID。

有人可以向我解释背后的原因是什么以及如何解决吗?

0 个答案:

没有答案