如何使用Microsoft.Azure.CognitiveServices.Knowledge.QnAMaker nuget版本1.0.0调用我的api qnamaker

时间:2019-05-09 10:48:30

标签: qnamaker

我正在开始使用Microsoft.Azure.CognitiveServices.Knowledge.QnAMaker nuget, 而且我正在尝试使用QnAMakerClient()类初始化QNAMakerClient类的新实例。

但是此类使用抽象参数: 公共QnAMakerClient(ServiceClientCredentials凭据,参数DelegatingHandler []处理程序)。  我找到了解决方法 https://csharp.hotexamples.com/examples/Microsoft.Rest/TokenCredentials/-/php-tokencredentials-class-examples.html,指示如何获取凭据令牌。

由于我是这个概念的新手,所以我不知道如何获取ServiceClientCredentials的凭据令牌。

1 个答案:

答案 0 :(得分:1)

您可以创建自己的 TokenCredentials 类,该类继承自 ServiceClientCredentials ,如下所示:

 public class TokenCredentials : ServiceClientCredentials
    {
         /// <summary>
        /// The bearer token type, as serialized in an http Authentication header.
        /// </summary>
        private const string BearerTokenType = "Bearer";

       /// <summary>
        /// Gets or sets secure token used to authenticate against Microsoft Azure API. 
        /// No anonymous requests are allowed.
        /// </summary>
        protected ITokenProvider TokenProvider { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="TokenCredentials"/>
        /// class with the given 'Bearer' token.
        /// </summary>
        /// <param name="token">Valid JSON Web Token (JWT).</param>
        public TokenCredentials(string token)
            : this(token, BearerTokenType)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TokenCredentials"/>
        /// class with the given token and token type.
        /// </summary>
        /// <param name="token">Valid JSON Web Token (JWT).</param>
        /// <param name="tokenType">The token type of the given token.</param>
        public TokenCredentials(string token, string tokenType) 
            : this(new StringTokenProvider(token, tokenType))
        {
            if (string.IsNullOrEmpty(token))
            {
                throw new ArgumentNullException("token");
            }
            if (string.IsNullOrEmpty(tokenType))
            {
                throw new ArgumentNullException("tokenType");
            }
        }

        /// <summary>
        /// Create an access token credentials object, given an interface to a token source.
        /// </summary>
        /// <param name="tokenProvider">The source of tokens for these credentials.</param>
        public TokenCredentials(ITokenProvider tokenProvider)
        {
            if (tokenProvider == null)
            {
                throw new ArgumentNullException("tokenProvider");
            }

            this.TokenProvider = tokenProvider;
        }

        /// <summary>
        /// Apply the credentials to the HTTP request.
        /// </summary>
        /// <param name="request">The HTTP request.</param>
        /// <param name="cancellationToken">Cancellation token.</param>
        /// <returns>
        /// Task that will complete when processing has completed.
        /// </returns>
        public async override Task ProcessHttpRequestAsync(HttpRequestMessage request,
            CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            if (TokenProvider == null)
            {
                throw new InvalidOperationException(Resources.TokenProviderCannotBeNull);
            }

            request.Headers.Authorization = await TokenProvider.GetAuthenticationHeaderAsync(cancellationToken);
            await base.ProcessHttpRequestAsync(request, cancellationToken);
        }
    }

This是了解QnAMaker的一个很好的起点。

希望这会有所帮助!