-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathflickrOAuth.py
121 lines (99 loc) · 3.68 KB
/
flickrOAuth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import oauth2 as oauth
import time
import httplib2
import urlparse
class APIKeys:
"""Helper class to read the API Keys"""
def __init__(self,filename):
if filename is None:
raise ValueError("No File Specified")
self.filename = filename
try:
fp = open("apikeys")
except IOError as e:
if e.errno == errno.EACCESS:
print "file does not exists"
# Not a permission error.
raise
else:
with fp:
self.apikey = fp.readline().rstrip('\n')
self.apisecret = fp.readline().rstrip('\n')
fp.close()
# Read the APIKeys
keys = APIKeys("apikeys")
# Set the API endpoint
url = "http://www.flickr.com/services/oauth/request_token"
authorize_url = "http://www.flickr.com/services/oauth/authorize"
access_token_url = "http://www.flickr.com/services/oauth/access_token"
# Set the base oauth_* parameters along with any other parameters required
# for the API call.
params = {
'oauth_timestamp': str(int(time.time())),
'oauth_signature_method':"HMAC-SHA1",
'oauth_version': "1.0",
'oauth_callback': "http://www.mkelsey.com",
'oauth_nonce': oauth.generate_nonce(),
'oauth_consumer_key': keys.apikey
}
print keys.apikey
print keys.apisecret
# Setup the Consumer with the api_keys given by the provider
consumer = oauth.Consumer(key=keys.apikey, secret=keys.apisecret)
# Create our request. Change method, etc. accordingly.
req = oauth.Request(method="GET", url=url, parameters=params)
# Create the signature
signature = oauth.SignatureMethod_HMAC_SHA1().sign(req,consumer,None)
# Add the Signature to the request
req['oauth_signature'] = signature
# Make the request to get the oauth_token and the oauth_token_secret
# I had to directly use the httplib2 here, instead of the oauth library.
print req.to_url()
h = httplib2.Http(".cache")
resp, content = h.request(req.to_url(), "GET")
#parse the content
request_token = dict(urlparse.parse_qsl(content))
print "Request Token:"
print " - oauth_token = %s" % request_token['oauth_token']
print " - oauth_token_secret = %s" % request_token['oauth_token_secret']
print
# Create the token object with returned oauth_token and oauth_token_secret
token = oauth.Token(request_token['oauth_token'], request_token['oauth_token_secret'])
# You need to authorize this app via your browser.
print "Go to the following link in your browser:"
print "%s?oauth_token=%s&perms=read" % (authorize_url, request_token['oauth_token'])
print
# Once you get the verified pin, input it
accepted = 'n'
while accepted.lower() == 'n':
accepted = raw_input('Have you authorized me? (y/n) ')
oauth_verifier = raw_input('What is the PIN? ')
#set the oauth_verifier token
token.set_verifier(oauth_verifier)
# Now you need to exchange your Request Token for an Access Token
# Set the base oauth_* parameters along with any other parameters required
# for the API call.
access_token_parms = {
'oauth_consumer_key': keys.apikey,
'oauth_nonce': oauth.generate_nonce(),
'oauth_signature_method':"HMAC-SHA1",
'oauth_timestamp': str(int(time.time())),
'oauth_token':request_token['oauth_token'],
'oauth_verifier' : oauth_verifier
}
#setup request
req = oauth.Request(method="GET", url=access_token_url, parameters=access_token_parms)
#create the signature
signature = oauth.SignatureMethod_HMAC_SHA1().sign(req,consumer,token)
# assign the signature to the request
req['oauth_signature'] = signature
#make the request
h = httplib2.Http(".cache")
resp, content = h.request(req.to_url(), "GET")
#parse the response
access_token_resp = dict(urlparse.parse_qsl(content))
#write out a file with the oauth_token and oauth_token_secret
with open('token', 'w') as f:
f.write(access_token_resp['oauth_token'] + '\n')
f.write(access_token_resp['oauth_token_secret'])
f.closed