[][src]Crate reqwest_oauth1

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;
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

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?;

    // 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(())
}

Structs

Client

Compatible interface with reqwest's Client.

OAuthParameters

Represents OAuth parameters including oauth_nonce, oauth_timestamp, realm, and others.

RequestBuilder

Compatible interface with reqwest's RequestBuilder.

Secrets

Represents OAuth secrets including consumer_key, consumer_secret, token, and token_secret. The token and token_secret are optional.

Signer

Provides OAuth signature with oauth1-request.

TokenResponse

Represents response of token acquisition.

Enums

Error

The Error bundles the TokenReaderError, SignError, and reqwest::Error.

SignerError

Errors about the signing with OAuth1 protocol.

TokenReaderError

Errors thrown from token_reader.

Constants

OAUTH_CALLBACK_KEY

Represents oauth_callback.

OAUTH_NONCE_KEY

Represents oauth_nonce.

OAUTH_TIMESTAMP_KEY

Represents oauth_timestamp.

OAUTH_VERIFIER_KEY

Represents oauth_verifier.

OAUTH_VERSION_KEY

Represents oauth_version.

REALM_KEY

Represents realm.

Traits

OAuthClientProvider

Bridge trait from reqwest's Client from our Client.

SecretsProvider

Interface of OAuth secrets provider

TokenReader

Add parse_oauth_token feature to reqwest::Response.

TokenReaderFuture

Add parse_oauth_token feature to Future of reqwest::Response.

Type Definitions

Result

Result type bound with Error.

SignResult

Result type bound with SignError.

TokenReaderResult

Result type bound with TokenReaderError.