Crate reqwest_oauth1
source ·Expand description
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 crate by providing the thin (partial-)compatible interface layer built with oauth1-request crate.
§How to use
§Basic usecase 1 - sending the tweet
use reqwest;
#[cfg(feature = "blocking")]
use reqwest::blocking::multipart;
#[cfg(feature = "blocking")]
use reqwest::blocking::Client;
#[cfg(not(feature = "blocking"))]
use reqwest::multipart;
#[cfg(not(feature = "blocking"))]
use reqwest::Client;
use reqwest_oauth1::*;
// 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 = Client::new();
let resp = client
// enable OAuth1 request
.oauth1(secrets)
.post(endpoint)
.multipart(content)
.send();
§Basic usecase 2 - Acquiring OAuth token & secret
use std::io;
use reqwest;
use reqwest_oauth1::{OAuthClientProvider, TokenReaderFuture};
async fn acquire_twitter_key() -> Result<(), reqwest_oauth1::Error> {
#[cfg(feature = "blocking")]
use reqwest::blocking::Client as Client;
#[cfg(not(feature = "blocking"))]
use reqwest::Client;
// 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 = Client::new();
#[cfg(not(feature = "blocking"))]
let resp = client
.oauth1(secrets)
.get(endpoint_reqtoken)
.query(&[("oauth_callback", "oob")])
.send()
.parse_oauth_token()
.await?;
#[cfg(feature = "blocking")]
let resp = client
.oauth1(secrets)
.get(endpoint_reqtoken)
.query(&[("oauth_callback", "oob")])
.send()
.parse_oauth_token()?;
/*
or
#[cfg(not(feature = "blocking"))]
let resp = client
.oauth1(secrets)
.get(endpoint_reqtoken)
.query(&[("oauth_callback", "oob")])
.send()
.await?;
#[cfg(feature = "blocking")]
let resp = client
.oauth1(secrets)
.get(endpoint_reqtoken)
.query(&[("oauth_callback", "oob")])
.send()?;
#[cfg(not(feature = "blocking"))]
let resp = serde_urlencoded::from_str::<reqwest_oauth1::TokenResponse>(resp.text().as_str()).unwrap();
#[cfg(feature = "blocking")]
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 = Client::new();
#[cfg(not(feature = "blocking"))]
let resp = client
.oauth1(secrets)
.get(endpoint_acctoken)
.query(&[("oauth_verifier", pin)])
.send()
.parse_oauth_token()
.await?;
#[cfg(feature = "blocking")]
let resp = client
.oauth1(secrets)
.get(endpoint_acctoken)
.query(&[("oauth_verifier", pin)])
.send()
.parse_oauth_token()?;
println!(
"your token and secret is: \n token: {}\n secret: {}",
resp.oauth_token, resp.oauth_token_secret
);
println!("other attributes: {:#?}", resp.remain);
Ok(())
}
Structs§
- Compatible interface with reqwest’s
Client
. - The
HMAC-SHA1
signature method. - Represents OAuth parameters including oauth_nonce, oauth_timestamp, realm, and others.
- Compatible interface with reqwest’s
RequestBuilder
. - Represents OAuth secrets including consumer_key, consumer_secret, token, and token_secret. The token and token_secret are optional.
- Provides OAuth signature with oauth1-request.
- Represents response of token acquisition.
Enums§
- The Error bundles the TokenReaderError, SignError, and reqwest::Error.
- Errors about the signing with OAuth1 protocol.
- Errors thrown from token_reader.
Constants§
- Represents
oauth_callback
. - Represents
oauth_nonce
. - Represents
oauth_timestamp
. - Represents
oauth_verifier
. - Represents
oauth_version
. - Represents
realm
.
Traits§
- Bridge trait from reqwest’s
Client
from ourClient
. - Interface of OAuth secrets provider
- Add parse_oauth_token feature to reqwest::Response.
- Add parse_oauth_token feature to Future of reqwest::Response.
Type Aliases§
- Result type bound with
Error
. - Result type bound with
SignError
. - Result type bound with
TokenReaderError
.