BotBuilder, Direct Line API returns “tokenParameters is missing User” with Enhanced authentication options

蓝咒 提交于 2019-12-07 19:47:39

问题


I have a bot published on an Azure account, from which I'm trying to take the magic code out, so following the Direct Line documentation I improved the code to hide the token. But once the Enhanced authentication options are enabled I always get the same response.

{
  "error": {
    "code": "BadArgument",
    "message": "tokenParameters is missing User."
  }
}

And I am not able to figure out how to complete the HTTP Request with the user data.

The bot is based on BotFramework SDK v4, plus some controllers for requesting and refreshing the token using the encapsulted secret key. I added userId data to the request by different and wrong ways getting always the same result.

Requesting token code

server.post('/dl/tokenRequest', async (_, res) => {
  try {
    const userId = "dl_testuser1";
    const askToken = await fetch('https://directline.botframework.com/v3/directline/tokens/generate', {
      headers: {
        authorization: Bearer ${ process.env.DIRECT_LINE_SECRET }
      },
      //HERE THE userId INFORMATION,
      method: 'POST'
    });
    const json = await askToken.json();
    if ('error' in json) {
      console.log('Requesting token - Error');
      res.send(500);
    } else {
      console.log(`Requesting token ` + json.token);
      res.send(json);
    }
  } catch (err) {
    res.send(500);
  }
});

How should I put the user's information to obtain an OK from the DL API as I received before enabling the Enhanced tool?


回答1:


It looks like you are missing the Content-Type in your header. Take a look at the code snippet below.

try {
  const res = await fetch('https://directline.botframework.com/v3/directline/tokens/generate', 
    { 
      method: 'POST',
      headers: {
        'Authorization': `Bearer <SECRET>`,
        'Content-Type': 'application/json'
      },
      body: {
        user: { 
          id: "dl_123", // user id must start with 'dl_'
          name: "user" 
        }
      }
    });

    const json = await res.json();

} catch (error) {
  console.log(error)
}

Note, you shouldn't have to check is 'error' is in the JSON response - the try-catch block should take care of that.

Hope this helps!



来源:https://stackoverflow.com/questions/56026764/botbuilder-direct-line-api-returns-tokenparameters-is-missing-user-with-enhan

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!