Richard Thibault
Richard Thibault

Reputation: 1

May I get a Google Chat API Java example to import data into Google Chat?

I would like to import data (spaces and messages) into Google Chat. I'm using Java.. I got some information using this doc (https://developers.google.com/chat/api/guides/import-data?hl=en#create_a_space_in_import_mode), but I couldn't acheave to make it work.

But I get this exception :

com.google.auth.oauth2.GoogleAuthException: Error getting access token for service account: 401 Unauthorized POST https://oauth2.googleapis.com/token, iss: SERVICE-ACCOUNT-EMAIL

I'm lost on what to exactly create as role and scopes even after reading the doc... Thank you If you can help me !

Here is the source code I used :

String googleChatApplicationName = "Migration App";
String service_account_credentials = "MyServiceAccount-key.json";
String service_account_ID = "SERVICE-ACCOUNT-EMAIL";
String CHAT_APP_SCOPE = "https://www.googleapis.com/auth/chat.import";

FileInputStream input = new FileInputStream(service_account_credentials);
GoogleCredentials credentials = GoogleCredentials.fromStream(input).createScoped(
    CHAT_APP_SCOPE).createDelegated(service_account_ID);
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credentials);
HangoutsChat chatService = new HangoutsChat.Builder(
    GoogleNetHttpTransport.newTrustedTransport(),
    GsonFactory.getDefaultInstance(),
    requestInitializer)
    .setApplicationName(googleChatApplicationName)
    .build();

Space space = new Space();
space.setDisplayName("[Teams]Created From App (display name)");
space.setExternalUserAllowed(true);
space.setName("[Teams]Created From App (name)");
chatService.spaces().create(space).execute();

Best regards

Upvotes: 0

Views: 602

Answers (2)

Richard Thibault
Richard Thibault

Reputation: 1

OK, I found the solution. "Error getting access token for service account: 401 Unauthorized" is a typical error of the delegated user.

  1. The delegated user must be an existing google workspace user : it must be its primary e-mail
  2. The scope defined in the Java code must be part of the scope list here (you must have only one scope per call) : developers.google.com/chat/api/guides/auth
  3. The scope has to be declared in the delegation for the service-account here : admin.google.com/ac/owl/domainwidedelegation
String googleChatApplicationName = "Migration App";
String service_account_credentials = "MyServiceAccount-key.json";
String service_account_ID = "SERVICE-ACCOUNT-EMAIL";
String delegated_user_account_email= "DELEGATED-GOOGLE-WORKSPACE-USER-EMAIL";
String CHAT_APP_SCOPE = "https://www.googleapis.com/auth/chat.spaces";

FileInputStream input = new FileInputStream(service_account_credentials);
GoogleCredentials credentials = GoogleCredentials.fromStream(input).createScoped(
    CHAT_APP_SCOPE).createDelegated(delegated_user_account_email);
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credentials);
HangoutsChat chatService = new HangoutsChat.Builder(
    GoogleNetHttpTransport.newTrustedTransport(),
    GsonFactory.getDefaultInstance(),
    requestInitializer)
    .setApplicationName(googleChatApplicationName)
    .build();

Space space = new Space();
space.setDisplayName("[Teams]Created From App (display name)");
space.setExternalUserAllowed(true);
space.setName("[Teams]Created From App (name)");
chatService.spaces().create(space).execute();

Upvotes: 0

kutycoi123
kutycoi123

Reputation: 13

I think you just need to replace service_account_ID with a real in-domain user email. The reason is that the space creation API requires user authentication (https://developers.google.com/chat/api/reference/rest/v1/spaces/create), so you need to pass a real user email in createDelegated.

Upvotes: 0

Related Questions