【问题标题】:403 Forbidden when accessing Kroger API from Azure-Hosted .Net Core 3.1 app从 Azure 托管的 .Net Core 3.1 应用程序访问 Kroger API 时被禁止 403
【发布时间】:2020-02-08 19:44:02
【问题描述】:

问题:我需要确定此故障的性质,以便知道如何进一步排除故障。我提出了几个假设: - 可能是 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-core https httpclient


    【解决方案1】:

    “标题”:[{ “密钥”:“服务器”, “价值”:[“AkamaiGHost”] }

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

    【讨论】:

    • 感谢您的回复。我完全忘记了这个问题。事实证明,无论它是什么问题,都会随着时间的推移而得到解决。我能找到的少量信息也指向了 Akamai,但几乎没有关于如何解决它的信息。
    猜你喜欢
    • 1970-01-01
    • 2020-10-13
    • 2021-02-19
    • 2017-09-24
    • 2021-01-11
    • 2020-09-15
    • 1970-01-01
    • 2019-11-29
    • 1970-01-01
    相关资源
    最近更新 更多