gpt4 book ai didi

azure - 尝试使用 ARM API 获取 token 时未授权

转载 作者:行者123 更新时间:2023-12-03 04:27:17 27 4
gpt4 key购买 nike

我尝试使用Azure Resource Manager API获取 token ,但得到401-Unauthorized响应。我的代码如下:

var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
"Basic",
Convert.ToBase64String(
System.Text.ASCIIEncoding.ASCII.GetBytes(
string.Format("{0}:{1}", client_Id, client_secret))));

var content = new FormUrlEncodedContent(new KeyValuePair<string, string>[]{

new KeyValuePair<string, string>("grant_type", "client_credentials")
});

content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");

var response = client.PostAsync("https://login.windows.net/subscriptionId/oauth2/token", content);

最佳答案

根据您的代码,您可以引用以下代码来获取您的token:

var client = new HttpClient();

var content = new FormUrlEncodedContent(new KeyValuePair<string, string>[]{
new KeyValuePair<string, string>("resource", "https://management.core.windows.net/"),
new KeyValuePair<string, string>("grant_type", "client_credentials"),
new KeyValuePair<string, string>("client_id", "{ClientID}"),
new KeyValuePair<string, string>("client_secret", "{ClientSecret}")
});

client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = await client.PostAsync("https://login.windows.net/{TennantID}/oauth2/token", content);

Console.WriteLine(await response.Content.ReadAsStringAsync());

结果:

enter image description here

更多详情可以引用这个blog关于使用 Azure ARM REST API – 获取访问 token 。

关于azure - 尝试使用 ARM API 获取 token 时未授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41736162/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com