googleapis / google-api-nodejs-client

Google's officially supported Node.js client library for accessing Google APIs. Support for authorization and authentication with OAuth 2.0, API Keys and JWT (Service Tokens) is included.
https://googleapis.dev/nodejs/googleapis/latest/
Apache License 2.0
11.26k stars 1.91k forks source link

Google Chat Spaces Create #3507

Open liamcharmer opened 1 week ago

liamcharmer commented 1 week ago

Hi i've been trying to create a google chat space automated via server. I am trying to use service account to do this request here;

However it comes back with the error;

"this method doesnt support service account authentication/ Authenticate with a user account."

I have followed this hyperlink to create the service account and it says it can take up to 24 hours for changes. May this just be the case?

https://developers.google.com/workspace/chat/authenticate-authorize-chat-app

const { GoogleAuth } = require('google-auth-library');
const key = require('./service-account-key.json');
const chat = require('@googleapis/chat');

exports.createChatSpace = async (req, res) => {
  try {

  const scopes = [
    'https://www.googleapis.com/auth/chat.spaces.create',
  ];

    // const scopes = ['https://www.googleapis.com/auth/chat.spaces'];

    const auth = new GoogleAuth({
      credentials: key,
      scopes: scopes
    });

    const authClient = await auth.getClient();

  const chatClient = await chat.chat({version: 'v1', auth: authClient});

const response= await chatClient.spaces.setup({
    requestBody: {
      space: {
        spaceType: 'SPACE',
        displayName: 'ASTEST',
      },
      memberships: [
      ]
    }
  });

    res.status(200).send(response.data);
  } catch (error) {
    console.error('Error creating chat space:', error);
    res.status(500).send(error.message);
  }
};