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§

Enums§

  • The Error bundles the TokenReaderError, SignError, and reqwest::Error.
  • Errors about the signing with OAuth1 protocol.
  • Errors thrown from token_reader.

Constants§

Traits§

Type Aliases§