首页 > 解决方案 > 从 Azure 托管的 .Net Core 3.1 应用程序访问 Kroger API 时被禁止 403

问题描述

问题:我需要确定此故障的性质,以便知道如何进一步排除故障。我提出了一些假设: - 可能是 Azure 中的防火墙/代理配置 - 可能是 Kroger 的 API 配置错误 - 可能是 Azure 应用程序拒绝公共证书 - 可能与上述任何一个都完全无关

详细信息:我正在尝试连接到 Kroger 的开发人员 API。以下代码已针对本文进行了简化。(我之前一直在使用 IHttpClientFactory 来生成我的 HTTPClient)。这可以在本地工作,但是一旦它部署到 Azure Web 服务,我会看到一条 403 消息(这似乎来自 Azure,而不是外部 API):

You don't have permission to access http://api.kroger.com on this server.

同样的代码在 Azure 中通过 HTTPS 为其他 3rd 方 API 工作,所以我怀疑这个错误来自没有使用正确的客户端证书,所以 Azure 尝试通过 http 调用?

我已经尝试了很多方法来解决这个问题,包括将我从https://api.kroger.com下载的公共证书上传到我的 azure 应用服务。它似乎正确地提取了证书,但请求仍然失败并显示相同的消息。

相关代码如下(不使用客户端证书):

using(var client = _requestFactory.CreateClient()))
{
        client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("basic", _clientId);
        client.DefaultRequestHeaders.Add("Accept", "application/json");

        var newRequest = new Dictionary<string, string>
        {
            { "grant_type", "client_credentials" },
            { "scope", "product.compact" }
        };

        var response = await client.PostAsync($"https://api.kroger.com/v1/connect/oauth2/token", new FormUrlEncodedContent(newRequest));
        return Ok(await response.Content.ReadAsStringAsync());
}

以下是来自服务器的完整响应。

{
    "version": "1.1",
    "content": {
        "headers": [{
            "key": "Content-Type",
            "value": ["text/html"]
        }, {
            "key": "Content-Length",
            "value": ["299"]
        }, {
            "key": "Expires",
            "value": ["Sat, 08 Feb 2020 19:18:55 GMT"]
        }]
    },
    "statusCode": 403,
    "reasonPhrase": "Forbidden",
    "headers": [{
        "key": "Server",
        "value": ["AkamaiGHost"]
    }, {
        "key": "Mime-Version",
        "value": ["1.0"]
    }, {
        "key": "Date",
        "value": ["Sat, 08 Feb 2020 19:18:55 GMT"]
    }, {
        "key": "Connection",
        "value": ["close"]
    }, {
        "key": "Set-Cookie",
        "value": ["akaalb_Digital_ALB_API=~op=KT_Digital_API_KCVG_F5:api-kcvg|~rv=47~m=api-kcvg:0|~os=75b4a9ec926d2a9e67035451773cec6c~id=63ba4b3e2a027e4d53b693e2fded5ac3; path=/; HttpOnly; Secure; SameSite=None"]
    }],
    "trailingHeaders": [],
    "requestMessage": {
        "version": "1.1",
        "content": {
            "headers": [{
                "key": "Content-Type",
                "value": ["application/x-www-form-urlencoded"]
            }, {
                "key": "Content-Length",
                "value": ["51"]
            }]
        },
        "method": {
            "method": "POST"
        },
        "requestUri": "https://api.kroger.com/v1/connect/oauth2/token",
        "headers": [{
            "key": "Authorization",
            "value": ["basic {removed}"]
        }, {
            "key": "Accept",
            "value": ["application/json"]
        }, {
            "key": "Request-Context",
            "value": ["appId={removed}"]
        }, {
            "key": "Request-Id",
            "value": ["|{removed}"]
        }, {
            "key": "traceparent",
            "value": ["{removed}"]
        }],
        "properties": {}
    },
    "isSuccessStatusCode": false
}

标签: azure.net-corehttpshttpclient

解决方案


“标题”:[{“键”:“服务器”,“值”:[“AkamaiGHost”]}

几乎可以肯定是 Akamai 问题。由于标头顺序甚至是 Akamai 不喜欢的用户代理标头,我看到了 403 错误。


推荐阅读