我想获得授权承载标题用于OAuth目的,但阅读文档时看起来有点混乱
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错误:
hyper::header::HeaderFormat
类型没有实现特性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)
}
}
答案 0 :(得分:2)
查看Authorization
的文档,我们可以看到它确实实现了Header
:
impl<S: Scheme + Any> Header for Authorization<S>
where S::Err: 'static
所以你走在了正确的轨道上。我的猜测是你遇到了一些更加阴险的东西:同一个箱子的多个版本。
具体来说,我今天编制的镍版本(0.7.3)取决于超0.6.16。但是,如果我将hyper = "*"
添加到我的Cargo.toml,那么我也获取最新版本的hyper - 0.7.0。
由于看起来不直观,超0.7的项目与超0.6的项目不兼容。这对于超级也没有什么特别之处;所有板条箱都是如此。
如果你更新你的依赖关系以锁定镍想要的相同版本的超级,那么你应该好好去。
<强> 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() {}