Watson 视觉识别 API C# 授权

Watson Visual Recognition API C# Authorisation

尝试让 Watson Visual Recognition 使用 C#,但在尝试通过 API 对图像进行分类时出现未经授权的错误。我使用的凭据是 "Auto-generated service credentials".

我收到的错误是: ServiceResponseException:API 查询失败,状态码未授权:未授权

这是我的代码:

class Program
{
    static void Main(string[] args)
    {
        string apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        string versionDate = "2018-03-19";
        string endpoint = "https://gateway.watsonplatform.net/visual-recognition/api";

        VisualRecognitionService visualRecognition = new VisualRecognitionService(apiKey, versionDate);
        visualRecognition.SetEndpoint(endpoint);

        // throws error here
        var result = visualRecognition.Classify(url: "https://upload.wikimedia.org/wikipedia/commons/thumb/d/d9/Collage_of_Nine_Dogs.jpg/1023px-Collage_of_Nine_Dogs.jpg"); 
    }
}

此外,如果我能提供更多可能有帮助的信息,请告诉我

好的,在查看 node.js 实施的答案后,我找到了解决方案:

问题是我需要使用 Iam Api 密钥进行身份验证,可以这样做:

class Program
{
    static void Main(string[] args)
    {
        string apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        string versionDate = "2018-03-19";
        string endpoint = "https://gateway.watsonplatform.net/visual-recognition/api";

        VisualRecognitionService visualRecognition = new VisualRecognitionService(apiKey, versionDate);
        visualRecognition.SetEndpoint(endpoint);

        // updated to include token options with Iam Api Key
        TokenOptions options = new TokenOptions
        {
            IamApiKey = apiKey
        };
        visualRecognition.SetCredential(options);
        // end edit

        var result = visualRecognition.Classify(url: "https://upload.wikimedia.org/wikipedia/commons/thumb/d/d9/Collage_of_Nine_Dogs.jpg/1023px-Collage_of_Nine_Dogs.jpg");
    }
}

希望这对遇到类似问题的人有所帮助