oauth2 Module

class restkit.oauth2.Consumer(key, secret)

Bases: object

A consumer of OAuth-protected services.

The OAuth consumer is a “third-party” service that wants to access protected resources from an OAuth service provider on behalf of an end user. It’s kind of the OAuth client.

Usually a consumer must be registered with the service provider by the developer of the consumer software. As part of that process, the service provider gives the consumer a key and a secret with which the consumer software can identify itself to the service. The consumer will include its key in each request to identify itself, but will use its secret only when signing requests, to prove that the request is from that particular registered consumer.

Once registered, the consumer can then use its consumer credentials to ask the service provider for a request token, kicking off the OAuth authorization process.

key = None
secret = None
exception restkit.oauth2.Error(message='OAuth error occurred.')

Bases: exceptions.RuntimeError

Generic exception class.


A hack to get around the deprecation errors in 2.6.

exception restkit.oauth2.MissingSignature(message='OAuth error occurred.')

Bases: restkit.oauth2.Error

class restkit.oauth2.Request(method='GET', url=None, parameters=None, body='', is_form_encoded=False)

Bases: dict

The parameters and information for an HTTP request, suitable for authorizing with OAuth credentials.

When a consumer wants to access a service’s protected resources, it does so using a signed HTTP request identifying itself (the consumer) with its key, and providing an access token authorized by the end user to access those resources.

classmethod from_consumer_and_token(consumer, token=None, http_method='GET', http_url=None, parameters=None, body='', is_form_encoded=False)
classmethod from_request(http_method, http_url, headers=None, parameters=None, query_string=None)

Combines multiple parameter sources.

classmethod from_token_and_callback(token, callback=None, http_method='GET', http_url=None, parameters=None)

Get any non-OAuth parameters.


Return a string that contains the parameters that must be signed.

classmethod make_nonce()

Generate pseudorandom number.

classmethod make_timestamp()

Get seconds since epoch (UTC).

sign_request(signature_method, consumer, token)

Set the signature parameter to the result of sign.


Serialize as a header for an HTTPAuth request.


Serialize as post data for a POST request.


Serialize as a URL for a GET request.

version = '1.0'
class restkit.oauth2.SignatureMethod

Bases: object

A way of signing requests.

The OAuth protocol lets consumers and service providers pick a way to sign requests. This interface shows the methods expected by the other oauth modules for signing requests. Subclass it and implement its methods to provide a new way to sign requests.

check(request, consumer, token, signature)

Returns whether the given signature is the correct signature for the given consumer and token signing the given request.

sign(request, consumer, token)

Returns the signature for the given request, based on the consumer and token also provided.

You should use your implementation of signing_base() to build the message to sign. Otherwise it may be less useful for debugging.

signing_base(request, consumer, token)

Calculates the string that needs to be signed.

This method returns a 2-tuple containing the starting key for the signing and the message to be signed. The latter may be used in error messages to help clients debug their software.

class restkit.oauth2.SignatureMethod_HMAC_SHA1

Bases: restkit.oauth2.SignatureMethod

name = 'HMAC-SHA1'
sign(request, consumer, token)

Builds the base signature string.

signing_base(request, consumer, token)
class restkit.oauth2.SignatureMethod_PLAINTEXT

Bases: restkit.oauth2.SignatureMethod

name = 'PLAINTEXT'
sign(request, consumer, token)
signing_base(request, consumer, token)

Concatenates the consumer key and secret with the token’s secret.

class restkit.oauth2.Token(key, secret)

Bases: object

An OAuth credential used to request authorization or a protected resource.

Tokens in OAuth comprise a key and a secret. The key is included in requests to identify the token being used, but the secret is used only in the signature, to prove that the requester is who the server gave the token to.

When first negotiating the authorization, the consumer asks for a request token that the live user authorizes with the service provider. The consumer then exchanges the request token for an access token that can be used to access protected resources.

callback = None
callback_confirmed = None
static from_string(s)

Deserializes a token from a string like one returned by to_string().

key = None
secret = None

Returns this token as a plain string, suitable for storage.

The resulting string includes the token’s secret, so you should never send or store this string where a third party can read it.

verifier = None

Optional WWW-Authenticate header (401 error)

restkit.oauth2.build_xoauth_string(url, consumer, token=None)

Build an XOAUTH string for use in SMTP/IMPA authentication.


Escape a URL including any /.


Generate pseudorandom number.


Get seconds since epoch (UTC).


Generate pseudorandom number.


Convert to unicode, raise exception with instructive error message if s is not unicode, ascii, or utf-8.


Raise TypeError if x is a str containing non-utf8 bytes or if x is an iterable which contains such a str.


Raise TypeError if x is a str or if x is an iterable which contains a str.