Skip to content

The API is called without service account credentials. Instructions on how to set this up can be found at https://developers.google.com/hangouts/chat/how-tos/service-accounts #249

Open
@liyakhatalisheikh

Description

@liyakhatalisheikh

Created OAuth2 Credentials and API KEY for Google Chat API

GoogleClientSecrets clientSecrets = getGoogleClientSecrets();
GoogleAuthorizationCodeFlow googleAuthorizationCodeFlow = new GoogleAuthorizationCodeFlow.Builder(
                GoogleNetHttpTransport.newTrustedTransport(),
                JacksonFactory.getDefaultInstance(),
                clientSecrets,
                Arrays.asList(SCOPES)
        ).setDataStoreFactory(new FileDataStoreFactory(new java.io.File("tokens")))
.setAccessType("offline").build();
String authorizationUrl = googleAuthorizationCodeFlow.newAuthorizationUrl().setRedirectUri(REDIRECT_URI).build();

Got the authorizationURL and redirected it and code authroizationCode

AuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                new NetHttpTransport(),
                JacksonFactory.getDefaultInstance(),
                getGoogleClientSecrets(),
                Arrays.asList(SCOPES)
        ).build();
TokenResponse tokenResponse = flow.newTokenRequest(code)
        .setRedirectUri(REDIRECT_URI)
        .execute();
String accessToken = tokenResponse.getAccessToken();

Got accessToken using the above code

Map<String, Object> requestBody = new HashMap<>();
requestBody.put("text", "Hello from Google API");
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("Authorization", "Bearer " + accessToken);
headers.add("Content-Type", "application/json");
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity("https://chat.googleapis.com/v1/spaces/" + SPACE_ID + "?key=" + API_KEY, requestBody, String.class, headers);
System.out.println(stringResponseEntity);

Getting 403 - FORBIDDEN

Same issue with Service Account As well

Here are the scopes

private static final String[] SCOPES = {"https://www.googleapis.com/auth/chat.spaces", "https://www.googleapis.com/auth/chat.messages", "https://www.googleapis.com/auth/chat.messages.create"};

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions