1

我正在尝试使用 GoogleCredential 和 JacksonFactory 库为我的 gsuite 域的每个用户获取 jwt 访问令牌。

代码示例 -

GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
                .setJsonFactory(JSON_FACTORY)
                .setServiceAccountId(clientEmail)
                .setServiceAccountScopes(scopes)
                .setServiceAccountPrivateKeyFromP12File(privateKeyFile)
                .setServiceAccountUser(userEmail)
                .build();

            credential.refreshToken();
            String accessToken = credential.getAccessToken();

所有字段 - clientEmail、范围、键和 userEmail 既不为空也不为空

对于少数用户,我无法获得访问令牌并收到此错误

com.google.api.client.repackaged.com.google.common.base.Preconditions.checkNotNull(Preconditions.java:191) com.google.api.client.util.Preconditions.checkNotNull(Preconditions.java:127) com.google.api.client.json.jackson2.JacksonFactory.createJsonParser(JacksonFactory.java:96) com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:85) com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:81) com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:88) com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287) com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307) com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:269) com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489) com.hubble.hubbleEngine.policyTypes.OAuth.getJWTAccessToken(OAuth.java:815)

这只是第一次发生,我正在尝试获取访问令牌。当我尝试再次为所有用户获取访问令牌时,我能够为第一次抛出错误的用户获取访问令牌。

我调试了一下,发现错误是从 com.google.api.client.auth.oauth2.TokenRequest 中存在的以下函数生成的

public final HttpResponse executeUnparsed() throws IOException {
// must set clientAuthentication as last execute interceptor in case it needs to sign request
HttpRequestFactory requestFactory =
    transport.createRequestFactory(new HttpRequestInitializer() {

      public void initialize(HttpRequest request) throws IOException {
        if (requestInitializer != null) {
          requestInitializer.initialize(request);
        }
        final HttpExecuteInterceptor interceptor = request.getInterceptor();
        request.setInterceptor(new HttpExecuteInterceptor() {
          public void intercept(HttpRequest request) throws IOException {
            if (interceptor != null) {
              interceptor.intercept(request);
            }
            if (clientAuthentication != null) {
              clientAuthentication.intercept(request);
            }
          }
        });
      }
    });
// make request
HttpRequest request =
    requestFactory.buildPostRequest(tokenServerUrl, new UrlEncodedContent(this));
request.setParser(new JsonObjectParser(jsonFactory));
request.setThrowExceptionOnExecuteError(false);
HttpResponse response = request.execute();
if (response.isSuccessStatusCode()) {
  return response;
}
throw TokenResponseException.from(jsonFactory, response);

}

request.execute() 会点击“ https://accounts.google.com/o/oauth2/token ”来获取令牌,但它会抛出一些错误响应。因此,它会抛出最后提到的 TokenResponseException。在这里, response.getContent() 为空,因此发生了整个空异常。

有没有办法知道调用引发了哪种错误响应。(>300 或 <200)?或者为什么会发生这种情况?

4

0 回答 0