如何在.NET SDK Watson上使用API​​Key和URL进行身份验证

时间:2019-02-05 21:31:04

标签: c# ibm-watson speech-to-text

如何在.NET SDK Watson(https://gateway.watsonplatform.net/natural-language-understanding/api)上使用API​​Key和URL(https://github.com/watson-developer-cloud/dotnet-standard-sdk)进行身份验证。

SDK示例具有SpeechToTextService(username, password);,但是现在Watson Credential仅具有APIKey和URL。

    const string SERVICE_NAME = "speech_to_text";
    const string URL = "https://stream.watsonplatform.net/speech-to-text/api";

    public SpeechToTextService() : base(SERVICE_NAME) { }

    public SpeechToTextService(string userName, string password) : base(SERVICE_NAME, URL)
    {
        if (string.IsNullOrEmpty(userName))
            throw new ArgumentNullException(nameof(userName));

        if (string.IsNullOrEmpty(password))
            throw new ArgumentNullException(nameof(password));

        this.SetCredential(userName, password);
    }

    public SpeechToTextService(TokenOptions options) : base(SERVICE_NAME, URL)
    {
        if (string.IsNullOrEmpty(options.IamApiKey) && string.IsNullOrEmpty(options.IamAccessToken))
            throw new ArgumentNullException(nameof(options.IamAccessToken) + ", " + nameof(options.IamApiKey));
        if (!string.IsNullOrEmpty(options.ServiceUrl))
        {
            this.Endpoint = options.ServiceUrl;
        }
        else
        {
            options.ServiceUrl = this.Endpoint;
        }

        _tokenManager = new TokenManager(options);
    }

    public SpeechToTextService(IClient httpClient) : base(SERVICE_NAME, URL)
    {
        if (httpClient == null)
            throw new ArgumentNullException(nameof(httpClient));

        this.Client = httpClient;
    }

1 个答案:

答案 0 :(得分:1)

看看README中的IAM密钥示例。您可以像这样提供密钥和URL

IamApiKey = "<iam-apikey>",
ServiceUrl = "<service-endpoint>"

还要查看有关使用ibm-credentials.env文件的信息。