User96
User96

Reputation: 53

botframework webchat token post call returns 403

I was using this example for Integrating bot in webchat.

<!DOCTYPE html>
<html lang="en-US">
  <head>
    <title>Web Chat: Send welcome event</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script crossorigin="anonymous" src="https://cdn.botframework.com/botframework-webchat/latest/webchat.js"></script>
    <style>
      html,
      body {
        height: 100%;
      }

      body {
        margin: 0;
      }

      #webchat {
        height: 100%;
        width: 100%;
      }
    </style>
  </head>
  <body>
    <div id="webchat"></div>
    <script>
      (async function() {
        const res = await fetch('https://directline.botframework.com/v3/directline/tokens/generate', 
        { method: 'POST',
        headers: { Authorization: 'my webchat secret' }
        });
        const { token } = await res.json();
        const store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {
          if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
            dispatch({
              type: 'WEB_CHAT/SEND_EVENT',
              payload: {
                name: 'webchat/join',
                value: { language: window.navigator.language }
              }
            });
          }

          return next(action);
        });

        window.WebChat.renderWebChat(
          {
            directLine: window.WebChat.createDirectLine({ token }),
            store
          },
          document.getElementById('webchat')
        );

        document.querySelector('#webchat > *').focus();
      })().catch(err => console.error(err));
    </script>
  </body>
</html>

please find the error message in console

enter image description here

But when I try the same api url with my bot secret as Authorization in postman, I'm getting 200 OK response with 'conversationId', 'token' and 'expires_in' values.

What am I missing here in the post call ?

Upvotes: 1

Views: 290

Answers (1)

Manu
Manu

Reputation: 186

I think that you are probably missing the Bearer part in the Authorization header.

So, it should look like this:

headers: { Authorization: 'Bearer my webchat secret' }

Upvotes: 3

Related Questions