gpt4 book ai didi

azure - 无法以编程方式添加/更新 Azure 功能 key

转载 作者:行者123 更新时间:2023-12-03 06:31:28 28 4
gpt4 key购买 nike

当尝试以编程方式添加/更新功能键时,我收到以下错误:

StatusCode: 401, ReasonPhrase: 'Unauthorized'

代码:

执行以下代码会导致上述错误。

static void FunctionKey(string resourceGroupName, string functionAppName, string functionName, NameValuePair kv)
{
var resource = $"subscriptions/{SubscriptionId.Value}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/sites/{functionAppName}/functions/{functionName}/keys/{kv.Name}?api-version=2022-03-01";
var httpClient = new HttpClient() { BaseAddress = new Uri("https://management.azure.com/") };
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AuthToken.Value);
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var json = @"{
""Properties"": {
""Name"": ""ApiKey"",
""Value"": ""some_value""
}
}";

using (var content = new StringContent(json, Encoding.UTF8, "application/json"))
{
var response = httpClient.PostAsync(resource, content).Result;

if (!response.IsSuccessStatusCode)
throw new Exception($"Error: Failed to register function key for {functionName}");
}
}

研究:

我在文档 emulator 中执行此任务时成功了.

enter image description here

enter image description here

最佳答案

我尝试通过 Postman 在我的环境中重现相同的结果,并得到以下结果:

当我在不包含不记名 token 的情况下运行以下查询时,我收到相同的错误401 Unauthorized如下所示:

PUT https://management.azure.com/subscriptions/<subID>/resourceGroups/<rgname>/providers/Microsoft.Web/sites/<funcappname>/functions/<funcname>/keys/<keyname>?api-version=2022-03-01
{
"Properties":
{
"Name": "keyname",
"Value": "xxxxxxxxxxxx"
}
}

回应:

enter image description here

传递 token 后,我可以成功创建功能键,如下所示:

enter image description here

当我检查同一个门户时,srikey 出现在功能键下,如下所示:

enter image description here

In your case, you are using httpClient.PostAsync which meansPOST method.

当我使用 POST 方法进行以下查询时,我也遇到了 404 Not find 错误,如下所示:

POST https://management.azure.com/subscriptions/<subID>/resourceGroups/<rgname>/providers/Microsoft.Web/sites/<funcappname>/functions/<funcname>/keys/<keyname>?api-version=2022-03-01
{
"Properties":
{
"Name": "keyname",
"Value": "xxxxxxxxxxxx"
}
}

回应:

enter image description here

解决错误,请确保使用PUT方法,将httpClient.PostAsync方法更改为httpClient.PutAsync方法。

引用: HttpClient.PutAsync Method (System.Net.Http) | Microsoft

关于azure - 无法以编程方式添加/更新 Azure 功能 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75204048/

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