# 2013-02-17 johnpfeiffer
# http://oauth.net/code/ (python oauth v1 library as a reference)
# base_string http://googlecodesamples.com/oauth_playground/
# signature http://oauth.googlecode.com/svn/code/javascript/example/signature.html
METHOD = 'GET' # must be uppercase
BASE_URL = 'https://www.google.com/accounts/OAuthGetRequestToken'
SIGNATURE_METHOD = 'HMAC-SHA1'
OAUTH_VERSION = '1.0'
SCOPE = 'https://www.google.com/calendar/feeds/'
CONSUMER_SECRET = 'anonymous'
# TODO: xoauth_displayname parameter to improve google login prompt
import random
nonce_length = 9
oauth_nonce = ''.join( [str( random.randint( 0, 9 ) ) for i in range( nonce_length )] )
import time
oauth_timestamp = int( time.time() )
def encoded( s ):
import urllib
"""Escape a URL including any /."""
return urllib.quote( s, safe = '~' )
# The added ampersand is also encoded!
encoded_oauth_consumer_key_value_pair = encoded( 'oauth_consumer_key' + '=' + CONSUMER_SECRET + '&' )
encoded_oauth_nonce_key_value_pair = encoded( 'oauth_nonce' + '=' + oauth_nonce + '&' )
encoded_oauth_signature_method_key_value_pair = encoded( 'oauth_signature_method' + '=' + SIGNATURE_METHOD + '&' )
encoded_oauth_timestamp_key_value_pair = encoded( 'oauth_timestamp' + '=' + str( oauth_timestamp ) + '&' )
encoded_oauth_version_key_value_pair = encoded( 'oauth_version' + '=' + OAUTH_VERSION + '&' )
encoded_scope_key_value_pair = encoded( 'scope' + '=' + encoded( SCOPE ) )
# manually sorted alphabetically by key, this is essentially concatenating 3 parts: METHOD, URL, ENCODED_PARAMETERS
base_string = METHOD.upper() + '&' + encoded( BASE_URL ) + '&'
base_string = base_string + encoded_oauth_consumer_key_value_pair
base_string = base_string + encoded_oauth_nonce_key_value_pair
base_string = base_string + encoded_oauth_signature_method_key_value_pair
base_string = base_string + encoded_oauth_timestamp_key_value_pair
base_string = base_string + encoded_oauth_version_key_value_pair
base_string = base_string + encoded_scope_key_value_pair
print 'GOOD base_string: %s' % base_string
key = '%s&' % CONSUMER_SECRET
import hmac
import hashlib
import base64
hashed = hmac.new( key, base_string , hashlib.sha1 )
signature = base64.b64encode( hashed.digest() )
print 'GOOD SIGNATURE: %s' % signature
TARGET = BASE_URL + "?scope=" + encoded( SCOPE )
parameters_as_headers = dict()
authorization = dict()
authorization[ 'Authorization'] = 'OAuth realm="", oauth_nonce="%s", oauth_timestamp="%s", ' \
'oauth_consumer_key="%s", oauth_signature_method="%s", oauth_version="%s", oauth_signature="%s"' \
% ( oauth_nonce , oauth_timestamp , CONSUMER_SECRET , SIGNATURE_METHOD , OAUTH_VERSION , signature )
print 'AUTHORIZATION HEADERS: %s' % authorization
# http://docs.python-requests.org/en/latest/
import requests
http = requests.Session()
response = http.get( TARGET , headers = authorization )
# print response.headers
token_parameter = ''
token_secret_parameter = ''
if response.status_code != 200:
print '%s' % response.content
else:
token_and_secret = response.content
token_and_secret_list = token_and_secret.split( '&' )
token_parameter = token_and_secret_list[0]
token_secret_parameter = token_and_secret_list[-1]
print 'TOKEN PARAMETER = %s' % token_parameter
print 'SECRET PARAMETER = %s' % token_secret_parameter