无法访问IBM Tone Analyzer API? [英] Can't access IBM Tone Analyzer API?

查看:47
本文介绍了无法访问IBM Tone Analyzer API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Laravel应用程序中使用Tone Analyzer API.无论我尝试什么,我总是得到与 {"code":401,"error":未经授权"} 相同的响应.我怀疑我的问题是我无法弄清楚如何传递API密钥,但是官方文档毫无帮助,因为它仅包含在命令行中使用cURL的说明.我的代码当前看起来像这样(尽管我尝试了许多其他迭代.如果有人需要我,我也可以发布所有其他失败的尝试):

I'm trying to use the Tone Analyzer API in a Laravel application. No matter what I try, I always get the same response of {"code":401, "error": "Unauthorized"}. I suspect my issue is that I can't figure out how to pass in the API key, but the official documentation is no help whatsoever because it only contains instructions for using cURL in the command line. My code currently looks like this (though I have tried many many other iterations. If anyone needs me to I can post all the other unsuccessful attempts as well):

$response = Curl::to('https://gateway-wdc.watsonplatform.net/tone-analyzer/api/v3/tone?version=2017-09-21&sentences=false')
        ->withOption('HTTPHEADER', array(
            'Content-Type: application/json',
            'apikey: REDACTED'))
        ->withData(array('text' => $text))
        ->asJson()
        ->post();

我正在运行Laravel 5.8,并使用Ixudra的cURL库.我希望如果答案也使用此库,但老实说,在这一点上,我随时准备放弃并使用原始PHP,以便对任何答案表示赞赏.

I am running Laravel 5.8 and using Ixudra's cURL library. I would prefer if answers made use of this library too but honestly at this point I'm ready to give up and use vanilla PHP anyway so any answers are appreciated.

忍者我知道问题不是我的帐户/API密钥,因为我试图通过命令行访问API,并且它按预期工作.该问题仅在尝试从Laravel访问时出现.

Ninja edit: I know the problem is not my account / API key, because I have tried to access the API through the command line and it worked just as expected. The issue only arises when trying to access it from Laravel.

推荐答案

IBM Watson Services使用 Basic 格式的HTTP标头认证.因此,在终端中使用 curl ,您应该以 user:password 的格式传递 -u或--user 标志,否则您可以还以以下模式发送身份验证Http标头:基本用户:密码.

IBM Watson Services uses HTTP Header Authentication in Basic format. Therefore, using curl in the terminal, you should pass the-u or --user flag in the format user:password, or you can also send the Authentication Http Header in pattern: Basic user:password.

通过调整第二种形式的代码,您可以按照以下步骤进行操作:

By adjusting your code for this second form, you can do it as follows:

$response = Curl::to('https://gateway-wdc.watsonplatform.net/tone-analyzer/api/v3/tone?version=2017-09-21&sentences=false')
        ->withHeader('Content-Type: application/json')
        ->withHeader('Authorization: Basic apikey:YOUR_TOKEN_HERE')
        ->withData(array('text' => $text))
        ->asJson()
        ->post();

用您的音频分析器API访问令牌替换 YOUR_TOKEN_HERE .

Replace YOUR_TOKEN_HERE by your Tone Analyzer API access token.

https://developer.mozilla.org/docs/Web/HTTP/Authentication https://www.ibm.com/support/knowledgecenter/zh-CN/SSGMCP_5.3.0/com.ibm.cics.ts.internet.doc/topics/dfhtl2a.html

希望这会有所帮助!

这篇关于无法访问IBM Tone Analyzer API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆