Crate oauth1_request[][src]

Yet yet yet another OAuth 1 client library.

Usage

Creating a GET request:

extern crate oauth1_request as oauth;

let mut sign = oauth::Signer::new(
    "GET",
    "https://example.com/api/v1/get.json",
    "consumer_secret",
    "token_secret", // or `None`
);

// The parameters must be appended in the ascending ordering.
sign.append("abc", "value")
    .append("lmn", "something");

// Append `oauth_*` parameters.
let mut sign = sign.append_oauth_params(
    "consumer_key",
    &*oauth::Options::new()
        .token("token")
        .nonce("nonce")
        .timestamp(9999999999),
);

sign.append("qrs", "stuff")
    .append("xyz", "blah-blah");

let oauth::Request { authorization, data } = sign.finish();

assert_eq!(
    authorization,
    "OAuth \
     oauth_consumer_key=\"consumer_key\",\
     oauth_nonce=\"nonce\",\
     oauth_signature_method=\"HMAC-SHA1\",\
     oauth_timestamp=\"9999999999\",\
     oauth_token=\"token\",\
     oauth_version=\"1.0\",\
     oauth_signature=\"JeDlFImHxfukYP0e6P2fy63G6V4%3D\"",
);
assert_eq!(
    data,
    "https://example.com/api/v1/get.json?abc=value&lmn=something&qrs=stuff&xyz=blah-blah",
);

Creating an x-www-form-urlencoded request:

// Use `new_form` method to create an `x-www-form-urlencoded` string.
let mut sign = oauth::Signer::new_form(
    "POST",
    "https://example.com/api/v1/post.json",
    "consumer_secret",
    "token_secret", // or `None`
);

// ...
// (same as the above example...)

let oauth::Request { authorization, data } = sign.finish();

assert_eq!(
    authorization,
    "OAuth \
     oauth_consumer_key=\"consumer_key\",\
     oauth_nonce=\"nonce\",\
     oauth_signature_method=\"HMAC-SHA1\",\
     oauth_timestamp=\"9999999999\",\
     oauth_token=\"token\",\
     oauth_version=\"1.0\",\
     oauth_signature=\"3S3N5Dod9azPWhXZKh4h44bTp4Y%3D\"",
);
assert_eq!(
    data,
    "abc=value&lmn=something&qrs=stuff&xyz=blah-blah",
);

Using the convenience wrapper method:

let oauth::Request { authorization, data } = oauth::Request::new(
    "GET",
    "https://example.com/api/v1/get.json",
    "consumer_key",
    "consumer_secret",
    "token_secret",
    &*oauth::Options::new().token("token").nonce("nonce").timestamp(9999999999),
    Some(&[
        // Ordering doesn't matter here:
        ("xyz", "blah-blah"),
        ("qrs", "stuff"),
        ("abc", "value"),
        ("lmn", "something"),
    ].iter().cloned().collect()),
);

assert_eq!(
    data,
    "https://example.com/api/v1/get.json?abc=value&lmn=something&qrs=stuff&xyz=blah-blah",
);

Structs

Options

Optional OAuth parameters.

Request

A pair of an OAuth header and its corresponding query/form string.

Signer

A type that creates a signed Request.

Enums

NotReady

Represents the state of a Signer before append_oauth_params method is called and unready to finish.

Ready

Represents the state of a Signer after append_oauth_params method is called and ready to finish.