azure http trigger works fine locally, loads fine on cloud, throws error when given params

huangapple go评论109阅读模式
英文:

azure http trigger works fine locally, loads fine on cloud, throws error when given params

问题

I see that you want a Chinese translation of the provided text. Here's the translation:

这应该是一件容易的事情:

我有这个HTTP触发器:

public static class Function1
{
    [FunctionName("httphook")]
    // def link: localhost:7070/api/httphook?function=webhook&data=this_could_be_your_data
    public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log)
    {
     
        string function = req.Query["function"];
     
        if (function == "webhook")
        {
            // do the webhook
            string data = req.Query["data"];
            return new OkObjectResult("ja");
     
        }
        else
        {
            return new OkObjectResult("error");
        }
   
    }
}

当我在本地运行它并打开此链接时:

localhost:7070/api/httphook?function=webhook

我得到了 "ja"。

现在我使用VS将其上传到Azure(所有设置都是默认的,并且所有设置都是通过VS的GUI设置的)

并且页面在这里加载:

https://webhookcircles.azurewebsites.net

当您点击链接时,您将轻松看到页面上写着 "it works fine"。

现在给出最终链接

https://webhookcircles.azurewebsites.net/api/httphook?function=webhook

它返回:401页面不起作用。

有人可以告诉我为什么吗?

英文:

This should be an easy thing:

I have this http trigger:

public static class Function1
    {
        [FunctionName(&quot;httphook&quot;)]
        // def link: localhost:7070/api/httphook?function=webhook&amp;data=this_could_be_your_data
        public static async Task&lt;IActionResult&gt; Run([HttpTrigger(AuthorizationLevel.Function, &quot;get&quot;, &quot;post&quot;, Route = null)] HttpRequest req, ILogger log)
        {
     
            string function = req.Query[&quot;function&quot;];
     
            if (function == &quot;webhook&quot;)
            {
                // do the webhook
                string data = req.Query[&quot;data&quot;];
                return new OkObjectResult(&quot;ja&quot;);
     
            }
            else
            {
                return new OkObjectResult(&quot;error&quot;);
            }
   
        }
   
    }

When I run it locally and open this linK:

localhost:7070/api/httphook?function=webhook

I am being returned "ja".

Now I use VS to upload this to azure (all settings are default, and all is set up from the gui in vs)

and the page loads here:

https://webhookcircles.azurewebsites.net

When you click the link, youll easily see that the page says "it works fine".

Now giving it the final link

https://webhookcircles.azurewebsites.net/api/httphook?function=webhook

It returns: 401 page doenst work.

Can someone tell me why?

答案1

得分: 0

在将您的函数发布到Azure后,您需要使用函数密钥来访问URL,因为您正在使用AuthorizationLevel.Function

您可以按照下面所示的方式复制URL

azure http trigger works fine locally, loads fine on cloud, throws error when given params

或者您可以从下面获取函数密钥

azure http trigger works fine locally, loads fine on cloud, throws error when given params

现在您的URL将如下所示

https://webhookcircles.azurewebsites.net/api/httphook?code=&lt;function key here&gt;&amp;function=webhook

Output:

azure http trigger works fine locally, loads fine on cloud, throws error when given params

英文:

After publishing your function to azure, you need to use the function key in order to access the URL, as you are using AuthorizationLevel.Function

You can copy the URL as shown below

azure http trigger works fine locally, loads fine on cloud, throws error when given params

or you can get the function key from below

azure http trigger works fine locally, loads fine on cloud, throws error when given params

Now your URL will look like

https://webhookcircles.azurewebsites.net/api/httphook?code=&lt;function key here&gt;&amp;function=webhook

Output:

azure http trigger works fine locally, loads fine on cloud, throws error when given params

huangapple
  • 本文由 发表于 2023年8月4日 20:54:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76836114.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定