1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
/*!
reqwest-oauth1: reqwest ♡ oauth1-request.
Repository is here: https://github.com/karno/reqwest-oauth1
# Overview
This library provides OAuth 1.0a authorization capability to [reqwest](https://crates.io/crates/reqwest)
crate by providing the thin (partial-)compatible interface layer built with [oauth1-request](https://crates.io/crates/oauth1-request) crate.
# How to use
## Basic usecase 1 - sending the tweet
```rust
use reqwest;
use reqwest::multipart;
use reqwest_oauth1::OAuthClientProvider;
// prepare authorization info
let consumer_key = "[CONSUMER_KEY]";
let consumer_secret = "[CONSUMER_SECRET]";
let access_token = "[ACCESS_TOKEN]";
let token_secret = "[TOKEN_SECRET]";
let secrets = reqwest_oauth1::Secrets::new(consumer_key, consumer_secret)
.token(access_token, token_secret);
// sample: send new tweet to twitter
let endpoint = "https://api.twitter.com/1.1/statuses/update.json";
let content = multipart::Form::new()
.text("status", "Hello, Twitter!");
let client = reqwest::Client::new();
let resp = client
// enable OAuth1 request
.oauth1(secrets)
.post(endpoint)
.multipart(content)
.send();
```
## Basic usecase 2 - Acquiring OAuth token & secret
```rust
use std::io;
use reqwest;
use reqwest_oauth1::{OAuthClientProvider, TokenReaderFuture};
async fn acquire_twitter_key() -> Result<(), reqwest_oauth1::Error> {
// prepare authorization info
let consumer_key = "[CONSUMER_KEY]";
let consumer_secret = "[CONSUMER_SECRET]";
let secrets = reqwest_oauth1::Secrets::new(consumer_key, consumer_secret);
// sample: request access token to twitter
// step 1: acquire request token & token secret
let endpoint_reqtoken = "https://api.twitter.com/oauth/request_token";
let client = reqwest::Client::new();
let resp = client
.oauth1(secrets)
.get(endpoint_reqtoken)
.query(&[("oauth_callback", "oob")])
.send()
.parse_oauth_token()
.await?;
/*
or
let resp = client
.oauth1(secrets)
.get(endpoint_reqtoken)
.query(&[("oauth_callback", "oob")])
.send()
.await?;
let resp = serde_urlencoded::from_str::<reqwest_oauth1::TokenResponse>(resp.text().await?.as_str()).unwrap();
*/
// step 2. acquire user pin
let endpoint_authorize = "https://api.twitter.com/oauth/authorize?oauth_token=";
println!("please access to: {}{}", endpoint_authorize, resp.oauth_token);
println!("input pin: ");
let mut user_input = String::new();
io::stdin().read_line(&mut user_input)
.expect("Failed to read the user input");
let pin = user_input.trim();
// step 3. acquire access token
let secrets = reqwest_oauth1::Secrets::new(consumer_key, consumer_secret)
.token(resp.oauth_token, resp.oauth_token_secret);
let endpoint_acctoken = "https://api.twitter.com/oauth/access_token";
let client = reqwest::Client::new();
let resp = client
.oauth1(secrets)
.get(endpoint_acctoken)
.query(&[("oauth_verifier", pin)])
.send()
.parse_oauth_token()
.await?;
println!(
"your token and secret is: \n token: {}\n secret: {}",
resp.oauth_token, resp.oauth_token_secret
);
println!("other attributes: {:#?}", resp.remain);
Ok(())
}
```
*/
mod client;
mod error;
mod request;
mod secrets;
mod signer;
mod token_reader;
// #[cfg(test)]
// mod usage_test;
// exposed to external program
pub use client::{Client, OAuthClientProvider};
pub use error::{Error, Result, SignResult, SignerError, TokenReaderError, TokenReaderResult};
pub use request::RequestBuilder;
pub use secrets::{Secrets, SecretsProvider};
pub use signer::{OAuthParameters, Signer};
pub use token_reader::{TokenReader, TokenReaderFuture, TokenResponse};
// exposed constant variables
/// Represents `oauth_callback`.
pub const OAUTH_CALLBACK_KEY: &str = "oauth_callback";
/// Represents `oauth_nonce`.
pub const OAUTH_NONCE_KEY: &str = "oauth_nonce";
/// Represents `oauth_timestamp`.
pub const OAUTH_TIMESTAMP_KEY: &str = "oauth_timestamp";
/// Represents `oauth_verifier`.
pub const OAUTH_VERIFIER_KEY: &str = "oauth_verifier";
/// Represents `oauth_version`.
pub const OAUTH_VERSION_KEY: &str = "oauth_version";
/// Represents `realm`.
pub const REALM_KEY: &str = "realm";
// crate-private constant variables
pub(crate) const OAUTH_KEY_PREFIX: &str = "oauth_";
pub(crate) const OAUTH_SIGNATURE_METHOD_KEY: &str = "oauth_signature_method";
pub(crate) const OAUTH_CONSUMER_KEY: &str = "oauth_consumer_key";
pub(crate) const OAUTH_TOKEN_KEY: &str = "oauth_token";