0

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 emulator.

enter image description here

enter image description here

8
  • Is this the exact code? The 404 hints on an incorrect url, and I can see you might have forgotten to replace the values between {xxxx} in the url Commented Jan 22, 2023 at 22:18
  • I compared the URL in my automated test with the URL in the documentation emulator. They are identical. Commented Jan 23, 2023 at 0:27
  • @Mocas - You were right. My test setup was executing the logic before my actual test would execute. This changed the state of things. Now I'm battling a bad request response that I can hopefully resolve. Commented Jan 23, 2023 at 0:36
  • I now get a 401 unauthorized. It's probably because my bearer token is empty. Commented Jan 23, 2023 at 0:49
  • Update: I fixed the unauthorized and back to 'Not found'. I checked the URL that succeeded with the one that failed and they are still identical. Commented Jan 23, 2023 at 1:38

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

I have a similar question if you're available: stackoverflow.com/questions/75212448/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.