Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When I am running azure HTTP Trigger function i am getting 401 unauthorized

Tags:

I am trying to run Azure HTTP Trigger Azure Function and I am receiving a 401 Unauthorized. It was working fine for me earlier.

When I created the new function under the same Function App and copied the same code then also it is running fine but when I am trying to run my created function then I am getting the same error that I mentioned.

I'm seeing the following logs in the streaming service.

2018-07-02T07:09:41 Welcome, you are now connected to log-streaming service.

2018-07-02T07:09:48.893 [Info] Executing HTTP request: { "requestId": "53e54698-c46b-4cb6-9ed0-d042eaf9ec71", "method": "POST", "uri": "/api/Source/MPAA/false" }

2018-07-02T07:09:48.893 [Info] Executed HTTP request: { "requestId": "53e54698-c46b-4cb6-9ed0-d042eaf9ec71", "method": "POST", "uri": "/api/Source/MPAA/false", "authorizationLevel": "Anonymous", "status": "Unauthorized" }

like image 337
Sumit Garg Avatar asked Jul 02 '18 06:07

Sumit Garg


People also ask

Why is my azure not triggering?

The most common cause for Azure Functions not getting triggered is that triggers are not synced properly. You can sync triggers in one of three ways: Restart your function app in the Azure portal.

What is HTTP trigger in Azure function?

The HTTP trigger lets you invoke a function with an HTTP request. You can use an HTTP trigger to build serverless APIs and respond to webhooks. The default return value for an HTTP-triggered function is: HTTP 204 No Content with an empty body in Functions 2.

How do I manually trigger Azure?

Navigate to your function app in the Azure portal, select App Keys, and then the _master key. In the Edit key section, copy the key value to your clipboard, and then select OK. After copying the _master key, select Code + Test, and then select Logs.


2 Answers

This is how I solved the problem based on the cause correctly provided by Nick above. Do this if you don't want to have to open the Azure Function's GUI every time you push your code.

In the source code of the function:

[FunctionName("YourFunctionName")] public static async Task<IActionResult> Run(     [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,     ILogger log){ 

What I had to do was to change the default setting AuthorizationLevel.Function into AuthorizationLevel.Anonymous. The former only allows triggering from other Function apps, the later will let you trigger from the browser.

like image 107
Mr.K Avatar answered Sep 20 '22 03:09

Mr.K


If you are managing your code via the azure portal, then simply navigate to "Integrate" and change the "Authorization Level" drop-down to "Anonymous". enter image description here

If you are managing your code with source control integration (e.g. via git), add this to your function.json:

"authLevel": "anonymous" 

Full snippet of function.json:

{   "bindings": [     {       "name": "req",       "type": "httpTrigger",       "direction": "in",       "methods": [ "post" ],       "route": "Source/MPAA",       "authLevel": "anonymous"     },     {       "type": "http",       "name": "res",       "direction": "out"     }   ],   "disabled": false } 

Note: the above is just an example, you may have to tweak the route. Note: the /api is the default prefix, and can be modified in the host.json file.

like image 37
Nick Avatar answered Sep 20 '22 03:09

Nick