当我向googleapis发出请求时,收到400错误请求

问题描述 投票:2回答:1

我正在尝试获取特定用户的组列表(分发列表)。失败了。

请求是

GET /admin/directory/v1/groups HTTP/1.1 Host: www.googleapis.com Content-length: 0 Authorization: Bearer ya29.Il-9B-9Z88RKqK73dr-NyeeJnCz0PqlPrMgxlr0JO6gc2q2jcROxsu7y6EaTHAlRkJatv5l3kd_PIPHrB7wXeMvfe0YTXupzupJ8-PUyrRcCIBE_qpPP8V7NoM0JJp

但是我正在收到如下所示的错误响应。

{ "error": { "code": 400, "message": "Bad Request", "errors": [ { "domain": "global", "message": "Bad Request", "reason": "badRequest" } ] } }

不仅是在获取群组时,而且从googleapis.com进行的每个API调用都具有这种行为。

http-status-code-400 bad-request google-developers-console google-groups google-groups-api
1个回答
0
投票

关于某些Google API的使用,我有同样的错误/困惑。

我假设您使用的服务帐户具有域范围内的委派访问权限?

[假设是的,问题在于,除了服务帐户凭据之外,您还需要设置一个用户,以便在发出请求时Google凭据将impersonate。也就是说,根据我的理解,域范围的委派只允许您将自己视为实际用户之一。

除了直接使用构建器外,我看不到任何通过google基本api进行设置的方法。这是一个示例:

  static GoogleCredential createCredential(HttpTransport transport, JsonFactory jsonFactory) throws Exception {
    final String credentialJsonPath = System.getProperty("user.home") + "/.config/gcloud/service-cred-from-console.json";
    try (InputStream credentialStream = new FileInputStream(credentialJsonPath)) {
      final GoogleCredential firstPassCred = GoogleCredential.fromStream(credentialStream, transport, jsonFactory);

      return new GoogleCredential.Builder()
          .setTransport(transport)
          .setJsonFactory(jsonFactory)
          .setServiceAccountId(firstPassCred.getServiceAccountId())
          .setServiceAccountProjectId(firstPassCred.getServiceAccountProjectId())
          .setServiceAccountPrivateKey(firstPassCred.getServiceAccountPrivateKey())
          .setServiceAccountPrivateKeyId(firstPassCred.getServiceAccountPrivateKeyId())
          // service accounts must act on behalf of an actual user and have scope
          .setServiceAccountScopes(Lists.newArrayList(GmailScopes.GMAIL_COMPOSE, GmailScopes.GMAIL_SETTINGS_SHARING))
          .setServiceAccountUser("[email protected]")
          .build();
    }
  }

[重要部分定义了ServiceAccountUser(您模拟的人)和您为该用户承担的ServiceAccountScope(s)

我发布了example gmail api gist。如果需要,可以通过charles等本地代理运行此命令。 REST调用顺序。

© www.soinside.com 2019 - 2024. All rights reserved.