How to implement OAuth 2 Circuit REST API for Bots?

156 views Asked by At

How to implement OAuth 2 Circuit REST API for Bots? To use the client_id and client_secret. Thank you.

1

There are 1 answers

0
Roger Urscheler On

See https://circuit.github.io/oauth.html#client_credentials on the HTTP request to get the token. You can manually perform the /oauth/token request to get a token, or use any OAuth 2.0 library. The perform regular HTTP GET/POST requests using this OAuth token.

Here is an example that uses simple-oauth2 to get the token and then node-fetch to get the conversations.

const simpleOauth2 = require('simple-oauth2');
const fetch = require('node-fetch');
const DOMAIN = 'https://circuitsandbox.net';

const credentials = {
  client: {
    id: '<client_id>',
    secret: '<cient_secret>'
  },
  auth: {
    tokenHost: DOMAIN
  }
};

// Initialize the OAuth2 Library
const oauth2 = simpleOauth2.create(credentials);

(async () => {
  try {
    const { access_token: token } = await oauth2.clientCredentials.getToken({scope: 'ALL'})
    console.log('Access Token: ', token);

    const convs = await fetch(`${DOMAIN}/rest/conversations`, {
      headers: { 'Authorization': 'Bearer ' + token },
    }).then(res => res.json());

    console.log('Conversations:', convs);
  } catch (err) {
    console.error(err);
  }
})();