3

我想为 OAuth 目的获取 Authorization Bearer 标头,但是阅读文档看起来有点混乱

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)
    }
}

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

4

1 回答 1

2

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

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

所以你走在了正确的轨道上。我的猜测是你遇到了更阴险的事情:同一个箱子的多个版本。

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

尽管看起来不直观,hyper 0.7 中的项目与 hyper 0.6 中的项目不兼容。这也不是关于 hyper 的。对于所有板条箱都是如此。

如果您更新您的依赖项以锁定与镍想要的相同版本的超级,那么您应该很高兴。


货运.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() {}
于 2015-12-16T17:23:50.923 回答