Chatbot OAuth 2.0 authentication

When you use the AI chatbot with private knowledge bases or knowledge bases with a mix of public and private content, you can use OAuth 2.0 authentication to control access.

To use OAuth2, you'll need to:

  1. Generate a client ID and secret within KnowledgeOwl.
  2. Request an OAuth access token from the OAuth token endpoint.
  3. Embed the OAuth 2.0 authentication embed code in the external site where you want to use the AI chatbot.

Generate client secret

To retrieve an OAuth2 token, you will first need to generate the Client Secret for your knowledge base:

  1. Go to KB settings > AI Chatbot.
  2. In the External website embed section, check the OAuth 2.0 authentication checkbox.
  3. Copy both the Client ID and the Client secret and store them for use in your application.
  4. If there are no client ID and secret displayed, select Generate OAuth secret to generate them.

 Continue to the next step in OAuth token request.

OAuth token request

Now that you have your client secret and client ID, use them to request an access token from the OAuth token endpoint. Below, we walk through two code samples on how to work with this endpoint, as well as a sample output.

Once you have your token request working, add the Oauth 2.0 authentication embed code to your external site.

Here's a sample CURL request to the OAuth token endpoint. To use this sample, replace clientID:clientSecret in row 2 with the client ID and client secret you saved in the previous step.

/** CURL EXAMPLE **/
curl -u clientID:clientSecret https://app.knowledgeowl.com/oauth2/token
-d "grant_type=client_credentials&scope=chatbot:interact&reader[ssoid]=UID&reader[username]=reader@mysite.com&reader[groups]=Group1,Group2"

Here's a sample JavaScript request to the OAuth token endpoint. To use this sample, replace yourClientID in row 2 and yourClientSecret in row 3 with the client ID and client secret you saved in the previous step.

/** JS EXAMPLE **/
const clientID = 'yourClientID';
const clientSecret = 'yourClientSecret';
const credentials = btoa(`${clientID}:${clientSecret}`);

fetch('https://app.knowledgeowl.com/oauth2/token', {
  method: 'POST',
  headers: {
    'Authorization': `Basic ${credentials}`,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    grant_type: 'client_credentials',
    scope: 'chatbot:interact',
    reader: {
      ssoid: 'UID',
      username: 'reader@mysite.com',
      groups: 'Group1,Group2'
    }
  })
})

Example response

The OAuth token endpoint returns a token in this format:

{
 "access_token":"1234567890987654321234567890987654321234",
 "expires_in":3600,
 "token_type":"Bearer",
 "scope":"chatbot:interact"
}

Embed in external site

In the Embed code section, copy the OAuth 2.0 authentication embed code.

This code includes two examples of how you can use it--one for if the token is available on page load, one where you set the token after page load with event handling. Choose the one that best fits your application workflow. 

Then add this embed code to the external site where you want the chatbot to appear.