错误:未经授权-IBM Watson Visual Recognition的Python脚本
所以我试图获取IBM视觉识别服务的输出,但总是得到相同的错误:{code:401,Error:Unauthorized} 如果我尝试使用cURL,它会起作用:错误:未经授权-IBM Watson Visual Recognition的Python脚本,python,http,authorization,visual-recognition,Python,Http,Authorization,Visual Recognition,所以我试图获取IBM视觉识别服务的输出,但总是得到相同的错误:{code:401,Error:Unauthorized} 如果我尝试使用cURL,它会起作用: $ curl -X POST -u "apikey: ------------" -F "images_file=@bobross.jpg" "https://gateway.watsonplatform.net/visual-recognition/api/v3/detect_faces?version=2018-03-19" { fa
$ curl -X POST -u "apikey: ------------" -F "images_file=@bobross.jpg" "https://gateway.watsonplatform.net/visual-recognition/api/v3/detect_faces?version=2018-03-19"
{ facerecognition data }
到目前为止,我的python代码:
import json
import sys
import requests
header= { 'apikey': '---------', 'Content-Type': 'FaceCharacteristics'}
url= "https://gateway.watsonplatform.net/visual-recognition/api/v3/detect_faces?version=2018-03-19"
file ={image:open('bobross.jpg','rb')}
r = requests.post(url, headers=header, files=file)
print(r.text)
我在其他变体中尝试了我的代码,但它总是导致未经授权的错误。
顺便说一句,我对python的经验很少,我仍在努力学习。在您的curl示例中,您正在使用-u标志传递身份验证,而在python中,您正在按原样在标头中传递它。服务器正在忽略标头中的身份验证,并且正如我们所期望的那样,将返回401 为了简化工作,我们可以使用 auth='apikey','[一个API键]'作为命名参数 从标题中删除Content-Type:FaceCharacteristics也是值得的——不太确定这是从哪里提取的
import requests
url = 'https://gateway.watsonplatform.net/visual-recognition/api/v3/classify?version=2018-03-19'
files = {'images_file': open('fruitbowl.jpg','rb')}
resp = requests.post(url, auth=('apikey', '[An API Key]'), files=files)
print(resp.content)
最后添加文件,您应该已经全部设置好了
但是,如果您正在做的事情不止这些
你可能想看看这张照片。
它有更多的文档和示例代码供您使用
例如,这是提供的
import json
from watson_developer_cloud import VisualRecognitionV3
visual_recognition = = VisualRecognitionV3(
version='{version}',
api_key='{api_key}'
)
with open('./fruitbowl.jpg', 'rb') as images_file:
classes = visual_recognition.classify(
images_file,
threshold='0.6',
classifier_ids='dogsx2018x03x17_1725181949,Connectors_424118776')
print(json.dumps(classes, indent=2))
欢迎来到堆栈溢出!请不要使用图像传达文字信息。您的问题是将它们替换为相应的代码。此外,寻求调试帮助的问题为什么这段代码不起作用?必须包括所需的行为、特定的问题或错误以及在问题本身中重现这些问题所需的最短代码。如果没有这一点,你的问题就偏离主题,可能会被关闭。请构造一个并包含它。非常感谢,它与请求中auth变量的实现一起工作!谢谢