reqwest_oauth1/
lib.rs

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