简体   繁体   English

Wit.ai API调用在客户端失败

[英]Wit.ai API Call Fails in Client

I am trying to make an API request to Wit.ai via javascript (ReactJS). 我正在尝试通过javascript(ReactJS)向Wit.ai发出API请求。 My browser network tab shows the call fails with the message: 我的浏览器网络选项卡显示呼叫失败并显示以下消息:

"error" : "Bad auth, check token/params" “错误”:“错误的身份验证,检查令牌/参数”

However, that same call shows as successful in the Wit.ai logs. 但是,该调用在Wit.ai日志中显示为成功。 I've verified the credentials are correct and I can successfully cUrl the call via terminal. 我已经验证了凭据正确无误,并且可以通过终端成功调用该呼叫。

Here's the call: 这是电话:

async action() {
    const resp = await fetch('https://api.wit.ai/message?v=20160526&q=hello', {
      method: 'GET',
      headers: {
        'Authorization': "Bearer " + accessToken
      },
      dataType: 'jsonp',
      mode: 'no-cors',
      credentials: 'include'
    }).then(resp => resp.json()).catch(e => console.log('Boo', e));
}

Since it's a JSONP request, you'll be having "Unexpected end of input" even though the request has been performed correctly. 由于这是JSONP请求,因此即使请求已正确执行,您也会遇到“输入的意外结束”。 I'm not sure there's even any way to make it work without proxying the request through your app server. 我不确定如果不通过您的应用服务器代理请求,是否还有任何方法可以使其正常工作。 Anyway, for this kind of request, remove headers completely and move access token into query string as access_token query param: 无论如何,对于这种请求,请完全删除headers并将访问令牌作为access_token查询参数移动到查询字符串中:

await fetch(`https://api.wit.ai/message?v=20160526&q=hello&access_token=${accessToken}`, {
  method: 'GET',
  dataType: 'jsonp',
  mode: 'no-cors',
  credentials: 'include'
}).then(resp => resp.json()).catch(e => console.log('Boo', e));

Check out "Network" tab on your browser to see that the request has been resolved successfully, even though the catch block has been reached during the execution of this fetch call. 在浏览器的“网络”标签上,即使在执行此fetch调用期间已到达catch块,也可以看到请求已成功解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM