Azure功能401 RestSharp的未授权问题

时间:2019-07-03 17:11:48

标签: c# azure-functions restsharp

我一直在使用两个不同的Azure函数互相通信,一个是包含在一个应用程序服务中的HttpTrigger,另一个是包含在另一个应用程序服务中的TimerTrigger。 HttpTrigger使用功能级别的身份验证。

这是我的HttpTrigger函数:

    [FunctionName("PendingOrders")]
    public static async Task<HttpResponseMessage> GetPendingOrderIds(
        [HttpTrigger(AuthorizationLevel.Function, "get", Route =  "pending-orders")]
        HttpRequestMessage req,
        TraceWriter log,
        [Inject] OrdersController controller)
    {

使用主键调用它可以在浏览器中使用,但不能与RestSharp一起使用。自上次部署以来,我唯一更改的是将RestSharp从106.3.1升级到106.6.9。不是主要版本的更改,因此不应有任何重大更改。

    private static IRestResponse ProcessRequest(string url, Method method,  object requestBody)
    {
        // Variable "url" is the path plus `?code=<master key here>
        var client = PrepareRequest(url, method, requestBody); // see below
        return client.Execute(); // executes here, returns 401 Unauthorized.
    }

    private static PreparedClient PrepareRequest(string url, Method method,  object requestBody)
    {
        var uri = new Uri(url);
        var client = new RestClient(uri.GetLeftPart(UriPartial.Authority));
        var request = new RestRequest(uri.PathAndQuery, method)
        {
            OnBeforeDeserialization = resp =>
            {
                resp.ContentType = "application/json";
            }
        };
        if (requestBody != null)
        {
            request.AddJsonBody(requestBody);
        }
        return PreparedClient.Prepare(client, request);
    }

PreparedClient在哪里

internal struct PreparedClient
{
    public IRestClient Client { get; private set; }
    public IRestRequest Request { get; private set; }
    public static PreparedClient Prepare(IRestClient client, IRestRequest  request)
    {
        var prepared = new PreparedClient { Client = client, Request =  request };
        return prepared;
    }
    public IRestResponse Execute()
    {
        return Client.Execute(Request);
    }
    public async Task<IRestResponse> ExecuteAsync()
    {
        return await Client.ExecuteTaskAsync(Request);
    }
}

我已经调试了代码,并验证了?code=xxx主密钥已添加到URL中。是否需要使用其他方法进行身份验证,我需要使用在RestSharp 106.3.1和106.6.9之间更改的身份?是否必须在标题中放入一些内容?

0 个答案:

没有答案