Package oauth2client :: Module client :: Class OAuth2Credentials
[hide private]
[frames] | no frames]

Class OAuth2Credentials

source code


Credentials object for OAuth 2.0.

Credentials can be applied to an httplib2.Http object using the authorize()
method, which then adds the OAuth 2.0 access token to each request.

OAuth2Credentials objects may be safely pickled and unpickled.

Instance Methods [hide private]
 
__init__(self, access_token, client_id, client_secret, refresh_token, token_expiry, token_uri, user_agent, id_token=None)
Create an instance of OAuth2Credentials.
source code
 
authorize(self, http)
Authorize an httplib2.Http instance with these credentials.
source code
 
refresh(self, http)
Forces a refresh of the access_token.
source code
 
apply(self, headers)
Add the authorization to the headers.
source code
 
to_json(self)
Creating a JSON representation of an instance of Credentials.
source code
 
set_store(self, store)
Set the Storage for the credential.
source code
 
_updateFromCredential(self, other)
Update this Credential from another instance.
source code
 
__getstate__(self)
Trim the state down to something that can be pickled.
source code
 
__setstate__(self, state)
Reconstitute the state of the object from being pickled.
source code
 
_generate_refresh_request_body(self)
Generate the body that will be used in the refresh request.
source code
 
_generate_refresh_request_headers(self)
Generate the headers that will be used in the refresh request.
source code
 
_refresh(self, http_request)
Refreshes the access_token.
source code
 
_do_refresh_request(self, http_request)
Refresh the access_token using the refresh_token.
source code

Inherited from Credentials (private): _to_json

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Class Methods [hide private]
 
from_json(cls, s)
Instantiate a Credentials object from a JSON description of it.
source code

Inherited from Credentials: new_from_json

Class Variables [hide private]

Inherited from Credentials: NON_SERIALIZED_MEMBERS

Properties [hide private]
  access_token_expired
True if the credential is expired or invalid.

Inherited from object: __class__

Method Details [hide private]

__init__(self, access_token, client_id, client_secret, refresh_token, token_expiry, token_uri, user_agent, id_token=None)
(Constructor)

source code 
Create an instance of OAuth2Credentials.

This constructor is not usually called by the user, instead
OAuth2Credentials objects are instantiated by the OAuth2WebServerFlow.

Args:
  access_token: string, access token.
  client_id: string, client identifier.
  client_secret: string, client secret.
  refresh_token: string, refresh token.
  token_expiry: datetime, when the access_token expires.
  token_uri: string, URI of token endpoint.
  user_agent: string, The HTTP User-Agent to provide for this application.
  id_token: object, The identity of the resource owner.

Notes:
  store: callable, A callable that when passed a Credential
    will store the credential back to where it came from.
    This is needed to store the latest access_token if it
    has expired and been refreshed.

Overrides: object.__init__

authorize(self, http)

source code 
Authorize an httplib2.Http instance with these credentials.

The modified http.request method will add authentication headers to each
request and will refresh access_tokens when a 401 is received on a
request. In addition the http.request method has a credentials property,
http.request.credentials, which is the Credentials object that authorized
it.

Args:
   http: An instance of httplib2.Http
       or something that acts like it.

Returns:
   A modified instance of http that was passed in.

Example:

  h = httplib2.Http()
  h = credentials.authorize(h)

You can't create a new OAuth subclass of httplib2.Authenication
because it never gets passed the absolute URI, which is needed for
signing. So instead we have to overload 'request' with a closure
that adds in the Authorization header and then calls the original
version of 'request()'.

Overrides: Credentials.authorize

refresh(self, http)

source code 
Forces a refresh of the access_token.

Args:
  http: httplib2.Http, an http object to be used to make the refresh
    request.

Overrides: Credentials.refresh

apply(self, headers)

source code 
Add the authorization to the headers.

Args:
  headers: dict, the headers to add the Authorization header to.

Overrides: Credentials.apply

to_json(self)

source code 
Creating a JSON representation of an instance of Credentials.

Returns:
   string, a JSON representation of this instance, suitable to pass to
   from_json().

Overrides: Credentials.to_json
(inherited documentation)

from_json(cls, s)
Class Method

source code 
Instantiate a Credentials object from a JSON description of it. The JSON
should have been produced by calling .to_json() on the object.

Args:
  data: dict, A deserialized JSON object.

Returns:
  An instance of a Credentials subclass.

Overrides: Credentials.from_json

set_store(self, store)

source code 
Set the Storage for the credential.

Args:
  store: Storage, an implementation of Stroage object.
    This is needed to store the latest access_token if it
    has expired and been refreshed.  This implementation uses
    locking to check for updates before updating the
    access_token.

_refresh(self, http_request)

source code 
Refreshes the access_token.

This method first checks by reading the Storage object if available.
If a refresh is still needed, it holds the Storage lock until the
refresh is completed.

Args:
  http_request: callable, a callable that matches the method signature of
    httplib2.Http.request, used to make the refresh request.

Raises:
  AccessTokenRefreshError: When the refresh fails.

_do_refresh_request(self, http_request)

source code 
Refresh the access_token using the refresh_token.

Args:
  http_request: callable, a callable that matches the method signature of
    httplib2.Http.request, used to make the refresh request.

Raises:
  AccessTokenRefreshError: When the refresh fails.


Property Details [hide private]

access_token_expired

True if the credential is expired or invalid.

If the token_expiry isn't set, we assume the token doesn't expire.

Get Method:
unreachable.access_token_expired(self) - True if the credential is expired or invalid.