Quickstart¶
Are you ready to start your application? This guide shows a small introduction about how to get started with pyoauth2. Before reading contents below, please make sure that you have already gone througth the previous section Installation.
Create a credential storage¶
Before starting OAuth 2.0 authorization process, a credential storage instance should be created for storing OAuth 2.0 related credential information such as access token, expire date and so on. pyoauth2 expects 1 storage for 1 client.
storage = FileStorage('test.dat')
To get credentials stored in the storage instance, you can use get()
method and for saving newly fetched or updated existing credentials you can use save()
method.
credentials = storage.get()
...
do_some_process(credentials)
...
storage.save(credentials)
Prepare OAuth 2.0 flow instance¶
Flow instance is the basic object for following OAuth 2.0 procedure.
flow = OAuth2AuthorizationFlow(required_params,
extra_auth_params=extra_auth_params,
extra_token_params=extra_token_params,
local=True)
flow.retrieve_authorization_code()
credentials = flow.retrieve_token()
Variables for required_params
, extra_auth_params
, extra_token_params
are dictionary which contains parameters for requests to the OAuth 2.0 server such as “Client id”, “Client secret”, “Redirect URI” and so on. See a complete example for keys and values for them.
Example code¶
Basically, only 2 objects above are required for authorization. Following code is a small but simple sample code for communicating with YouTube API.
# -*- coding: utf-8 -*-
from pyoauth2.client import OAuth2AuthorizationFlow, FileStorage, OAuth2APIRequest
class YouTubeAPIRequest(OAuth2APIRequest):
def __init__(self, access_token):
OAuth2APIRequest.__init__(self, access_token)
self.authorization_header = {
"Authorization": "Bearer %s" % self.access_token
}
if __name__ == '__main__':
yt_feed_uri = r"https://gdata.youtube.com/feeds/api/users/default/uploads"
required_params = {
'client_id': "XXXXXXXXXXXXX.apps.googleusercontent.com",
'client_secret': "XXXXXXXXXXXXXXXXXXX",
'auth_uri': "https://accounts.google.com/o/oauth2/auth",
'token_uri': "https://accounts.google.com/o/oauth2/token",
'scope': [r'http://gdata.youtube.com'],
'redirect_uri': "urn:ietf:wg:oauth:2.0:oob"
}
extra_auth_params = {
'response_type': "code",
'access_type': "offline"
}
extra_token_params = {
'grant_type': "authorization_code",
}
storage = FileStorage('youtube.dat')
credentials = storage.get()
if credentials is None:
flow = OAuth2AuthorizationFlow(required_params,
extra_auth_params,
extra_token_params,
True)
flow.retrieve_authorization_code()
credentials = flow.retrieve_token()
storage.save(credentials)
access_token = credentials['access_token']
req = YouTubeAPIRequest(access_token)
data = req.request(yt_feed_uri)
print data