语音服务 - 如何使用“授权:持票人”用C#语言进行身份验证? [英] Speech Service- How to use "Authorization: Bearer" to authenticate in C # language?

查看:79
本文介绍了语音服务 - 如何使用“授权:持票人”用C#语言进行身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序与"Ocp-Apim-Subscription-Key"完美配合。在哪里我报告了关键。但是现在我通过返回错误运行它时出现问题:"附加信息:远程服务器返回错误:(401)未经授权。"。


我看到它有"授权:持票人"。标题,但我不知道怎么做。



我的申请中没有改变任何内容,我只是开始犯错误。



谢谢。

My application was working perfectly with "Ocp-Apim-Subscription-Key" where I reported the key. But now I'm having problems running it by returning the error: "Additional information: The remote server returned an error: (401) Unauthorized.".

I saw that it has the "Authorization: Bearer" header, but I do not know how to do it.

I have not changed anything in my application, I just start making mistakes.

Thanks.

推荐答案

您好,

您可以使用此示例来获取令牌。 

You can use this sample to get the token. 

public class Authentication
{
    public static readonly string FetchTokenUri =
        "https://westus.api.cognitive.microsoft.com/sts/v1.0/issueToken";
    private string subscriptionKey;
    private string token;

    public Authentication(string subscriptionKey)
    {
        this.subscriptionKey = subscriptionKey;
        this.token = FetchTokenAsync(FetchTokenUri, subscriptionKey).Result;
    }

    public string GetAccessToken()
    {
        return this.token;
    }

    private async Task<string> FetchTokenAsync(string fetchUri, string subscriptionKey)
    {
        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
            UriBuilder uriBuilder = new UriBuilder(fetchUri);

            var result = await client.PostAsync(uriBuilder.Uri.AbsoluteUri, null);
            Console.WriteLine("Token Uri: {0}", uriBuilder.Uri.AbsoluteUri);
            return await result.Content.ReadAsStringAsync();
        }
    }
}

你可以看看这个
文档
关于其他方法它。

You can take a look at this documentation on other ways to do it.


这篇关于语音服务 - 如何使用“授权:持票人”用C#语言进行身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆