从 Azure 托管的 .Net Core 3.1 应用程序访问 Kroger API 时禁止访问 403
403 Forbidden when accessing Kroger API from Azure-Hosted .Net Core 3.1 app
问题:我需要确定此故障的性质,以便知道如何进一步排除故障。我提出了几个假设:
- 可能是 Azure 中的 firewall/proxy 配置
- 可能是 Kroger 的配置错误 API
- 可能是来自 Azure 应用程序的 public 证书拒绝
- 可能与以上任何一项完全无关
详细信息:我正在尝试连接到 Kroger 的开发人员 API。以下代码已为此 post 进行了简化。 (我之前一直在使用 IHttpClientFactory 来生成我的 HTTPClient)。这在本地有效,但一旦部署到 Azure Web 服务,我就会看到一条 403 消息(似乎来自 Azure 而不是外部 API):
You don't have permission to access http://api.kroger.com on this server.
同样的代码在 Azure 中适用于通过 HTTPS 的其他第 3 方 API,所以我怀疑这个错误是由于没有使用正确的客户端证书,所以 Azure 试图通过 http 调用?
我尝试了很多方法来解决这个问题,包括将我从 https://api.kroger.com 下载的 public 证书上传到我的 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
}
"headers": [{
"key": "Server",
"value": ["AkamaiGHost"]
}
几乎可以肯定是 Akamai 问题。由于 header 订单甚至 Akamai 不喜欢的 user-Agent header,我看到了 403 错误。
问题:我需要确定此故障的性质,以便知道如何进一步排除故障。我提出了几个假设: - 可能是 Azure 中的 firewall/proxy 配置 - 可能是 Kroger 的配置错误 API - 可能是来自 Azure 应用程序的 public 证书拒绝 - 可能与以上任何一项完全无关
详细信息:我正在尝试连接到 Kroger 的开发人员 API。以下代码已为此 post 进行了简化。 (我之前一直在使用 IHttpClientFactory 来生成我的 HTTPClient)。这在本地有效,但一旦部署到 Azure Web 服务,我就会看到一条 403 消息(似乎来自 Azure 而不是外部 API):
You don't have permission to access http://api.kroger.com on this server.
同样的代码在 Azure 中适用于通过 HTTPS 的其他第 3 方 API,所以我怀疑这个错误是由于没有使用正确的客户端证书,所以 Azure 试图通过 http 调用?
我尝试了很多方法来解决这个问题,包括将我从 https://api.kroger.com 下载的 public 证书上传到我的 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
}
"headers": [{ "key": "Server", "value": ["AkamaiGHost"] }
几乎可以肯定是 Akamai 问题。由于 header 订单甚至 Akamai 不喜欢的 user-Agent header,我看到了 403 错误。