如何从.net核心调用ibm watson api

lag*_*fvu 4 .net curl ibm-watson personality-insights

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

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"
Run Code Online (Sandbox Code Playgroud)

taj*_*taj 5

您可以使用Watson Developer Cloud .NET Standard SDK.使用NuGet安装Personality Insights服务

Install-Package IBM.WatsonDeveloperCloud.PersonalityInsights -Pre
Run Code Online (Sandbox Code Playgroud)

实例化服务

// create a Personality Insights Service instance
PersonalityInsightsService _personalityInsights = new PersonalityInsightsService();

// set the credentials
_personalityInsights.SetCredential("<username>", "<password>");
Run Code Online (Sandbox Code Playgroud)

致电该服务

var results = _personalityInsights.GetProfile(ProfileOptions.CreateOptions()
                                                         .WithTextPlain()
                                                         .AsEnglish()
                                                         .AcceptJson()
                                                         .AcceptEnglishLanguage()
                                                         .WithBody("some text"));
Run Code Online (Sandbox Code Playgroud)

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

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


Say*_*chi 2

在这种情况下你使用curl来调用API吗?根据你的例子...

通过提供您要使用的服务实例的服务凭证中提供的username和来调用 Personality Insights 。API 使用基本身份验证。passwordHTTP

对于身份验证:

curl -u "{username}":"{password}"
"https://gateway.watsonplatform.net/personality-insights/api/v3/{method}"
Run Code Online (Sandbox Code Playgroud)

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

请求记录:

curl -u "{username}":"{password}"
--header "X-Watson-Learning-Opt-Out: true"
"https://gateway.watsonplatform.net/personality-insights/api/v3/{method}"
Run Code Online (Sandbox Code Playgroud)

调用并获取响应的方法:

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"
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

检查来自 IBM 的开发文档,其中包含如何调用的所有示例以及如果出现错误的原因。这是为了验证如何工作和如何使用。

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