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.

message

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_nonoauth_parameters()

Get any non-OAuth parameters.

get_normalized_parameters()

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

get_parameter(parameter)
classmethod make_nonce()

Generate pseudorandom number.

classmethod make_timestamp()

Get seconds since epoch (UTC).

method
sign_request(signature_method, consumer, token)

Set the signature parameter to the result of sign.

to_header(realm='')

Serialize as a header for an HTTPAuth request.

to_postdata()

Serialize as post data for a POST request.

to_url()

Serialize as a URL for a GET request.

url
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().

get_callback_url()
key = None
secret = None
set_callback(callback)
set_verifier(verifier=None)
to_string()

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
restkit.oauth2.build_authenticate_header(realm='')

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.

restkit.oauth2.escape(s)

Escape a URL including any /.

restkit.oauth2.generate_nonce(length=8)

Generate pseudorandom number.

restkit.oauth2.generate_timestamp()

Get seconds since epoch (UTC).

restkit.oauth2.generate_verifier(length=8)

Generate pseudorandom number.

restkit.oauth2.setter(attr)
restkit.oauth2.to_unicode(s)

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

restkit.oauth2.to_unicode_if_string(s)
restkit.oauth2.to_unicode_optional_iterator(x)

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

restkit.oauth2.to_utf8(s)
restkit.oauth2.to_utf8_if_string(s)
restkit.oauth2.to_utf8_optional_iterator(x)

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