如何从 .NET Core 调用 IBM Watson API



我正在尝试调用 Watson 个性洞察 api,环顾四周后,解决方案似乎是发出与以下 curl 请求等效的 .net 等效项。我对此很陌生,想知道我是否可以获得指导或指向相关教程。

curl -X POST -u "{username}:{password}"
--header "Content-Type: application/json"
--data-binary @profile
"https://gateway.watsonplatform.net/personality-insights/api/v3/profile?version=2016-10-20&consumption_preferences=true&raw_scores=true"

您可以使用 Watson Developer Cloud .NET Standard SDK。使用 NuGet 安装个性见解服务

Install-Package IBM.WatsonDeveloperCloud.PersonalityInsights -Pre

实例化服务

// create a Personality Insights Service instance
PersonalityInsightsService _personalityInsights = new PersonalityInsightsService();
// set the credentials
_personalityInsights.SetCredential("<username>", "<password>");

调用服务

var results = _personalityInsights.GetProfile(ProfileOptions.CreateOptions()
.WithTextPlain()
.AsEnglish()
.AcceptJson()
.AcceptEnglishLanguage()
.WithBody("some text"));

将来的版本中,您将能够使用命名参数而不是生成选项来调用服务。

var results = _personalityInsights.GetProfile(
"<input>", 
"<content-type>", 
"<content-language>", 
"<accept>", 
"<accept-language>",
"<raw-scores>",
"<csv-headers>"
"<consumption-preferences>",
"<version>"
);

在这种情况下,您是否使用 curl 来调用 API?根据您的示例...

通过提供要使用的服务实例的服务凭据中提供的usernamepassword来调用个性见解。API 使用HTTP基本身份验证。

对于身份验证:

curl -u "{username}":"{password}"
"https://gateway.watsonplatform.net/personality-insights/api/v3/{method}"

Bluemix 从所有请求中收集数据,并使用这些数据来改进 Watson 服务。

请求日志记录:

curl -u "{username}":"{password}"
--header "X-Watson-Learning-Opt-Out: true"
"https://gateway.watsonplatform.net/personality-insights/api/v3/{method}"

调用和获取响应的方法:

curl -X POST -u "{username}:{password}"
--header "Content-Type: application/json"
--data-binary @profile.json
"https://gateway.watsonplatform.net/personality-insights/api/v3/profile?version=2016-10-20&consumption_preferences=true&raw_scores=true"

IBM Watson API 使用标准 HTTP 响应代码来指示方法是否成功完成。

  • 200 级响应始终表示成功。

  • 400 级响应表示某种故障。

  • 500 级响应通常表示内部系统错误。

查看 IBM 要开发的此文档,其中包含如何调用的所有示例,如果有错误,请说明原因。这用于验证如何工作以及如何使用。

演示在这里,如果你愿意,你可以从github分叉。

最新更新