当我运行代码时,它说“ 406不可接受”,我该如何解决?

时间:2019-10-12 18:27:14

标签: python ibm-cloud ibm-watson

我在IBM云中进行搜索,在他们的错误处理中,他们说服务器不支持这种类型的文件

from ibm_watson import PersonalityInsightsV3
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
authenticator = IAMAuthenticator('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
personality_insights = PersonalityInsightsV3(
       version='2019-10-12',
authenticator=authenticator)
personality_insights.set_service_url('https://gateway-lon.watsonplatform.net/personality-insights/api')
profile_text = open("personality.txt").read()
profile = personality_insights.profile(profile_text,"text/plain").get_result()
needs = profile["needs"]
values = profile["values"]
personality = profile["personality"]
def print_traits(traits_category_name, traits):
  print(traits_category_name + ":")
for trait in traits:
  print(trait["name"] + ": {:.3f}%".format(
trait["percentile"] * 100))
print("\n")
print_traits("Needs", needs)
print_traits("Values", values)
print_traits("Personality", personality)

this is the code

and this the output

1 个答案:

答案 0 :(得分:0)

让我首先告诉您,在问题中共享您的凭据密钥已对您造成了危害,因此您应尽快将其撤消。

获得HTTP 406的原因是因为您提供了无效的accept类型的text/plain。如果查看服务的Python SDK API文档-https://cloud.ibm.com/apidocs/personality-insights?code=python#get-profile-您会看到第二个参数是accept,唯一有效的类型是application/json和{{1} }。

我认为您实际上是在尝试设置text/csv,因此您需要将服务调用到:

content_type