-
Notifications
You must be signed in to change notification settings - Fork 1
/
oauthorize.py
129 lines (97 loc) · 4.37 KB
/
oauthorize.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
121
122
123
124
125
126
127
128
129
#!/usr/bin/python
# oauthorize.py - A script to get an OAuth key/secret for a Weavr
# Copyright (C) 2012 Rob Myers <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Make sure you've set up the config.py file with your prosthetic's keys
# And make sure you copy the Weavr key/secret when you get them!!!
################################################################################
# Imports
################################################################################
import urlparse
import oauth2 as oauth
import os.path
import json
import urllib2
import urllib
import urlparse
import BaseHTTPServer
import webbrowser
import sys
import config
################################################################################
# Weavrs OAuth configuration
################################################################################
API_SERVER = 'www.weavrs.com'
OAUTH_SERVER_PATH = 'http://%s/oauth' % API_SERVER
REQUEST_TOKEN_URL = OAUTH_SERVER_PATH + '/request_token/'
ACCESS_TOKEN_URL = OAUTH_SERVER_PATH + '/access_token/'
AUTHORIZATION_URL = OAUTH_SERVER_PATH + '/authorize/'
CALLBACK_URL = 'http://127.0.0.1:8080/'
################################################################################
# Horrible, horrible globals
# Replace with something sane if this needs to be made more than a script
################################################################################
secret = None
serve = True
################################################################################
# Our local HTTP server, for completing OAuth authentication
################################################################################
class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(self):
global serve
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
request_token = urlparse.parse_qs(urlparse.urlparse(self.path).query)
# Get the access token
oauth_token = request_token.get('oauth_token')
# Make sure we're not being asked for a favicon or anything
if not oauth_token:
return
token = oauth.Token(oauth_token, secret) #request_token['oauth_token_secret'])
token.set_verifier(request_token['oauth_verifier'])
client = oauth.Client(consumer, token)
resp, content = client.request(ACCESS_TOKEN_URL, "POST")
access_token = dict(urlparse.parse_qsl(content))
print "Access Token:"
print " oauth_token = %s" % access_token['oauth_token']
print " oauth_token_secret = %s" % access_token['oauth_token_secret']
serve = False
################################################################################
# Main Flow Of Execution
################################################################################
# Make an OAuth client using the Prosthetic key
consumer = oauth.Consumer(config.consumer_key, config.consumer_secret)
client = oauth.Client(consumer)
# Get request token
resp, content = client.request(REQUEST_TOKEN_URL + '?oauth_callback=' +
urllib2.quote(CALLBACK_URL), "GET")
if resp['status'] != '200':
raise Exception("Invalid response %s." % resp['status'])
request_token = dict(urlparse.parse_qsl(content))
secret = request_token['oauth_token_secret']
# Reditect to OAuth server
auth_url = "%s?oauth_token=%s" %(AUTHORIZATION_URL,
request_token['oauth_token'])
# Try to play nice with the web browser in Gnome 3.2
try:
web_client = webbrowser.get('firefox')
web_client.open_new_tab(auth_url)
except:
webbrowser.open_new_tab(auth_url)
# Run a local web server to receive the authentication results from the browser
httpd = BaseHTTPServer.HTTPServer(('127.0.0.1', 8080), RequestHandler)
while serve:
httpd.handle_request()