Home > Back-end >  Unable to programmatically add/update Azure Function Key
Unable to programmatically add/update Azure Function Key

Time:01-23

When attempting to programmatically add/update a function key, I receive the following error:

StatusCode: 401, ReasonPhrase: 'Unauthorized'

Code:

Executing the following code results in the error described above.

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}");
    }
}

Research:

I was successful when performing this task in the the documentation enter image description here

enter image description here

CodePudding user response:

I tried to reproduce the same in my environment via Postman and got below results:

When I ran the below query without including bearer token, I got same error with 401 Unauthorized like below:

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"
        }
}   

Response:

enter image description here

After passing the token, I'm able to create function key successfully like below:

enter image description here

When I checked the same portal, srikey appeared under function keys like below:

enter image description here

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

When I used POST method for below query, I too got 404 Not found error like below:

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"
        }
}   

Response:

enter image description here

To resolve the error, make sure to use PUT method by changing httpClient.PostAsync method to httpClient.PutAsync method.

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

  • Related