Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 opened this issue Sep 11, 2023 · 0 comments
Assignees

Comments

@liyakhatalisheikh
Copy link

liyakhatalisheikh commented Sep 11, 2023

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"};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants