diff options
| author | DankDumpster <[email protected]> | 2021-01-14 08:19:30 +0100 |
|---|---|---|
| committer | DankDumpster <[email protected]> | 2021-01-14 08:19:30 +0100 |
| commit | ec7b0fbcd2f3273d51e15c5c1b34b45cb5a4f46f (patch) | |
| tree | d407e0ee19ce0551a558f6e757b613783ef06067 /examples | |
| parent | Initial commit (diff) | |
| download | oauth-ec7b0fbcd2f3273d51e15c5c1b34b45cb5a4f46f.tar.xz oauth-ec7b0fbcd2f3273d51e15c5c1b34b45cb5a4f46f.zip | |
added reqwest support
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/rocket.rs | 39 |
1 files changed, 15 insertions, 24 deletions
diff --git a/examples/rocket.rs b/examples/rocket.rs index d661e2f..13050ce 100644 --- a/examples/rocket.rs +++ b/examples/rocket.rs @@ -19,21 +19,14 @@ //! `$ git clone https://github.com/serenity-rs/oauth` //! `$ cd oauth` //! `$ DISCORD_CLIENT_SECRET=my_secret DISCORD_CLIENT_ID=my_client_id cargo run --example rocket` +#![feature(decl_macro)] -#![feature(custom_derive, plugin)] -#![plugin(rocket_codegen)] - -extern crate hyper; -extern crate hyper_native_tls; -extern crate serenity_oauth; +#[macro_use] extern crate rocket; -use hyper::net::HttpsConnector; -use hyper::Client as HyperClient; -use hyper_native_tls::NativeTlsClient; use rocket::response::Redirect; use serenity_oauth::model::AccessTokenExchangeRequest; -use serenity_oauth::{DiscordOAuthHyperRequester, Scope}; +use serenity_oauth::{DiscordOAuthReqwestRequester, Scope}; use std::env; use std::error::Error; @@ -50,25 +43,25 @@ fn get_client_id() -> u64 { } fn get_client_secret() -> String { - env::var("DISCORD_CLIENT_SECRET") - .expect("No DISCORD_CLIENT_SECRET present") + env::var("DISCORD_CLIENT_SECRET").expect("No DISCORD_CLIENT_SECRET present") } -#[get("/callback?<params>")] -fn get_callback(params: Params) -> Result<String, Box<Error>> { +#[get("/callback?<code>")] +fn get_callback(code: String) -> Result<String, Box<dyn Error>> { // Exchange the code for an access token. - let ssl = NativeTlsClient::new()?; - let connector = HttpsConnector::new(ssl); - let client = HyperClient::with_connector(connector); + let client = reqwest::blocking::Client::new(); let response = client.exchange_code(&AccessTokenExchangeRequest::new( get_client_id(), get_client_secret(), - params.code, + code, "http://localhost:8000/callback", ))?; - Ok(format!("The user's access token is: {}", response.access_token)) + Ok(format!( + "The user's access token is: {}", + response.access_token + )) } #[get("/")] @@ -82,13 +75,11 @@ fn get_redirect() -> Redirect { "http://localhost:8000/callback", ); - Redirect::to(&url) + Redirect::to(url) } fn main() { rocket::ignite() - .mount("/", routes![ - get_callback, - get_redirect, - ]).launch(); + .mount("/", routes![get_callback, get_redirect,]) + .launch(); } |