botframework 网络聊天令牌 post 调用 returns 403

botframework webchat token post call returns 403

我正在使用这个 example 在网络聊天中集成机器人。

<!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>

请在控制台中查找错误消息

但是当我在 postman 中尝试使用我的机器人秘密作为授权时 api url,我得到了 'conversationId' 的 200 OK 响应,'token' 和 'expires_in' 值。

我在 post 电话中错过了什么?

我认为您可能遗漏了 授权 header 中的 Bearer 部分。

所以,它应该是这样的:

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