获取服务帐户的访问令牌时出错:Oauth for Google Analytics API的400 Bad Request问题

n6lpvg4x  于 2023-08-02  发布在  Go
关注(0)|答案(1)|浏览(208)

我正试图用下面的代码打谷歌分析API。getAccessToken();返回null:

@RequestMapping(value = "/process", method = RequestMethod.POST, consumes = "text/plain")
    public ResponseEntity<String> process(@RequestBody String payload) throws Exception {
        GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream("C:\\Firm_Initiatve\\workspace-spring-tool-suite-4-4.8.1.RELEASE\\googleAnalyticsRest\\src\\main\\resources\\flowing-sign-294117-8998a8e44928.json"));
          if (credentials.createScopedRequired()) { 
              credentials = credentials.createScoped(Collections.singletonList("https://www.googleapis.com/auth/analytics")); 
              }
        credentials.refreshIfExpired();
        AccessToken token = credentials.getAccessToken();
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", token.toString());
        headers.setContentType(MediaType.APPLICATION_JSON);
        RestTemplate restTemplate = new RestTemplate();
        HttpEntity<String> request = new HttpEntity<String>(payload, headers);
        String personResultAsJsonString = restTemplate.postForObject(appURL, request, String.class);
        return new ResponseEntity<String>(personResultAsJsonString, HttpStatus.OK);

字符串

但它的错误与下面的错误:

java.io.IOException: Error getting access token for service account: 400 Bad Request
POST https://oauth2.googleapis.com/token
{"error":"invalid_grant","error_description":"Invalid JWT Signature."}
    at com.google.auth.oauth2.ServiceAccountCredentials.refreshAccessToken(ServiceAccountCredentials.java:444) ~[google-auth-library-oauth2-http-0.22.1.jar:na]
    at com.google.auth.oauth2.OAuth2Credentials.refresh(OAuth2Credentials.java:157) ~[google-auth-library-oauth2-http-0.22.1.jar:na]
    at com.google.auth.oauth2.OAuth2Credentials.refreshIfExpired(OAuth2Credentials.java:174) ~[google-auth-library-oauth2-http-0.22.1.jar:na]
    at com.example.demo.AnalyticsControllerOauth.process(AnalyticsControllerOauth.java:32) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_271]

ppcbkaq5

ppcbkaq51#

2023年为我解决了这个问题。
如果在Windows上,右键单击右下角的日期和时间。单击“调整日期/时间”。单击“立即同步。

相关问题