如何正确地将身份验证令牌放在 GET 请求的标头中

问题描述

我正在尝试使用 blockcypher.com 向 api 发出 get 请求。在文档中,他们只是将 api 令牌附加到带有 ?token= 的 URL 或者如果它是多个参数 &token= 的一部分。将其放入标头以确保出于安全原因不在 URL 中发送令牌的正确协议是什么?

我已经尝试过这个公式

var client = _clientFactory.CreateClient(nameof(<Parent Function Name>));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer",<API Token Here>);

这没有用。我的 blockcypher 帐户声称当我使用上述代码时没有向我的 API 发送请求。

解决方法

如果您调用的 API 不支持 @ViewBuilder func makeButton() -> some View { if let variable = value { Button(action: { //execute a function here },label: { Text("Other Label") .padding(.vertical,20) .frame(width: UIScreen.main.bounds.width * 0.8) .background(Color.white) }) } else { Button(action: { print("printing now...") },label: { Text("Label") }) } } 标头,则您无能为力。从查看 BlockCypher 的文档来看,它们似乎只支持将令牌作为 URL 参数传递。 Authorization 标头是非常标准的,所以不支持它很奇怪。

只要您仅进行服务器到服务器的通信(而不是浏览器或移动应用程序),这并没有您想象的那么大的安全风险。当您使用 HTTPS 时,请求 URL 是加密的(请参阅 Is an HTTPS query string secure?)。