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, revoke_uri=None, id_token=None, token_response=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
 
revoke(self, http)
Revokes a refresh_token and makes the credentials void.
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
 
access_token_expired(self)
True if the credential is expired or invalid.
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
 
_revoke(self, http_request)
Revokes the refresh_token and deletes the store if available.
source code
 
_do_revoke(self, http_request, token)
Revokes the credentials and deletes the store if available.
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]

Inherited from object: __class__

Method Details [hide private]

__init__(self, access_token, client_id, client_secret, refresh_token, token_expiry, token_uri, user_agent, revoke_uri=None, id_token=None, token_response=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.
  revoke_uri: string, URI for revoke endpoint. Defaults to None; a token
    can't be revoked if this is None.
  id_token: object, The identity of the resource owner.
  token_response: dict, the decoded response to the token request. None
    if a token hasn't been requested yet. Stored because some providers
    (e.g. wordpress.com) include extra fields that clients may want.

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.

Decorators:
  • @util.positional(8)
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

revoke(self, http)

source code 
Revokes a refresh_token and makes the credentials void.

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

Overrides: Credentials.revoke

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

access_token_expired(self)

source code 
True if the credential is expired or invalid.

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

Decorators:
  • @property

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.

_revoke(self, http_request)

source code 
Revokes the refresh_token and deletes the store if available.

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

_do_revoke(self, http_request, token)

source code 
Revokes the credentials and deletes the store if available.

Args:
  http_request: callable, a callable that matches the method signature of
    httplib2.Http.request, used to make the refresh request.
  token: A string used as the token to be revoked. Can be either an
    access_token or refresh_token.

Raises:
  TokenRevokeError: If the revoke request does not return with a 200 OK.