[][src]Crate oauth1_request

Yet yet yet another OAuth 1 client library.

Usage

Add this to your Cargo.toml:

[dependencies]
oauth = { version = "0.4", package = "oauth1-request" }

For brevity, we refer to the crate name as oauth throughout the documentation, since the API is designed in favor of qualified paths like oauth::get.

Create a request

A typical authorization flow looks like this:

// Define a type to represent your request.
#[derive(oauth::Request)]
struct CreateComment<'a> {
    article_id: u64,
    text: &'a str,
}

let uri = "https://example.com/api/v1/comments/create.json";

let request = CreateComment {
    article_id: 123456789,
    text: "A request signed with OAuth & Rust 🦀 🔏",
};

// Prepare your credentials.
let token =
    oauth::Token::from_parts("consumer_key", "consumer_secret", "token", "token_secret");

// Create the `Authorization` header.
let authorization_header = oauth::post(uri, &request, &token, oauth::HmacSha1);
// `oauth_nonce` and `oauth_timestamp` vary on each execution.
assert_eq!(
    authorization_header,
    "OAuth \
         oauth_consumer_key=\"consumer_key\",\
         oauth_nonce=\"Dk-OGluFEQ4f\",\
         oauth_signature_method=\"HMAC-SHA1\",\
         oauth_timestamp=\"1234567890\",\
         oauth_token=\"token\",\
         oauth_signature=\"n%2FrUgos4CFFZbZK8Z8wFR7drU4c%3D\"",
);

// You can create an `x-www-form-urlencoded` string or a URI with query pairs from the request.

let form = oauth::to_form_urlencoded(&request);
assert_eq!(
    form,
    "article_id=123456789&text=A%20request%20signed%20with%20OAuth%20%26%20Rust%20%F0%9F%A6%80%20%F0%9F%94%8F",
);

let uri = oauth::to_uri_query(uri.to_owned(), &request);
assert_eq!(
    uri,
    "https://example.com/api/v1/comments/create.json?article_id=123456789&text=A%20request%20signed%20with%20OAuth%20%26%20Rust%20%F0%9F%A6%80%20%F0%9F%94%8F",
);

Use oauth::Builder if you need to specify a callback URI or verifier:

let uri = "https://example.com/oauth/request_temp_credentials";
let callback = "https://client.example.net/oauth/callback";

let client = oauth::Credentials::new("consumer_key", "consumer_secret");

let authorization_header = oauth::Builder::<_, _>::new(client, oauth::HmacSha1)
    .callback(callback)
    .post(uri, &());

See Request for more details on the derive macro.

Re-exports

pub use oauth_credentials::Credentials;
pub use oauth_credentials::Token;
pub use signature_method::HmacSha1;
pub use signature_method::Plaintext;

Modules

serializer

Types to convert a Request to a signature or a URI query/form string.

signature_method

Signature methods (RFC 5849 section 3.4.).

Macros

skip_serialize_oauth_parameters

Helper macro for implementors of Serializer which generates blank implementation of serialize_oauth_* methods.

Structs

Builder

A builder for OAuth Authorization header string.

Traits

Request

Types that represent an HTTP request to be authorized with OAuth.

Functions

authorize

Authorizes a request to uri using the given credentials.

connect

Authorizes a CONNECT request to uri using the given credentials.

delete

Authorizes a DELETE request to uri using the given credentials.

get

Authorizes a GET request to uri using the given credentials.

head

Authorizes a HEAD request to uri using the given credentials.

options

Authorizes an OPTIONS request to uri using the given credentials.

patch

Authorizes a PATCH request to uri using the given credentials.

post

Authorizes a POST request to uri using the given credentials.

put

Authorizes a PUT request to uri using the given credentials.

to_form_urlencoded

Turns a Request into an x-www-form-urlencoded string.

to_uri_query

Turns a Request to a query string and appends it to the given URI.

trace

Authorizes a TRACE request to uri using the given credentials.

Derive Macros

Request

A derive macro for Request trait.