gpt4 book ai didi

oauth - 如何获取 Authorization Bearer header ?

转载 作者:行者123 更新时间:2023-11-29 08:08:01 29 4
gpt4 key购买 nike

我想获取用于 OAuth 目的的 Authorization Bearer header ,但阅读文档看起来有点困惑

use nickel::{Nickel, JsonBody, HttpRouter, Request, Response, MiddlewareResult, MediaType};

// Get the full Authorization header from the incoming request headers
let auth_header = match request.origin.headers.get::<Authorization<Bearer>>() {
Some(header) => header,
None => panic!("No authorization header found")
};

这会产生错误:

src/main.rs:84:56: 84:86 error: the trait hyper::header::HeaderFormat is not implemented for the type hyper::header::common::authorization::Authorization<hyper::header::common::authorization::Bearer> [E0277]

从实现来看,我认为是正确的:

https://github.com/hyperium/hyper/blob/master/src/header/common/authorization.rs

impl<S: Scheme + Any> HeaderFormat for Authorization<S> where <S as FromStr>::Err: 'static {
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(scheme) = <S as Scheme>::scheme() {
try!(write!(f, "{} ", scheme))
};
self.0.fmt_scheme(f)
}
}

https://github.com/auth0/rust-api-example/issues/1

最佳答案

查看 Authorization 的文档,我们可以看到它确实实现了Header:

impl<S: Scheme + Any> Header for Authorization<S>
where S::Err: 'static

所以你走对了路。我的猜测是您遇到了更阴险的事情:同一个 crate 的多个版本。

具体来说,我今天编译的nickel版本(0.7.3),依赖于hyper 0.6.16。但是,如果我将 hyper = "*" 添加到我的 Cargo.toml,那么我也会获得最新版本的 hyper - 0.7.0。

虽然看起来不直观,但 hyper 0.7 的项目与 hyper 0.6 的项目不兼容。这也不是关于 hyper 的具体内容;所有 crate 都是如此。

如果您更新依赖项以锁定到 nickel 想要的相同版本的 hyper,那么您就可以开始了。


Cargo.toml

# ...

[dependencies]
hyper = "0.6.16"
nickel = "*"

src/main.rs

extern crate nickel;
extern crate hyper;

use hyper::header::{Authorization, Bearer};
use nickel::{HttpRouter, Request};

fn foo(request: Request) {
// Get the full Authorization header from the incoming request headers
let auth_header = match request.origin.headers.get::<Authorization<Bearer>>() {
Some(header) => header,
None => panic!("No authorization header found")
};
}

fn main() {}

关于oauth - 如何获取 Authorization Bearer header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34317700/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com