forked from vstempel/EmotionStockMarket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogleauth.py
42 lines (30 loc) · 1.5 KB
/
googleauth.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
import urllib,json
baseurl='https://accounts.google.com/o/oauth2/auth?scope=%s&state=unique&redirect_uri=%s&response_type=code&client_id=%s'
scope=urllib.quote('https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email')
# These are all from the Google api console
client_id=urllib.quote('550850080377.apps.googleusercontent.com')
redirect_url=urllib.quote('http://ml7.stuycs.org:6007/auth2callback')
client_secret="YRuHw-xFTIYUONiSDo7Cok-5"
#later replace 127.0.0.1:5000 in redirect_url
original_url= 'http://ml7.stuycs.org:6007'
test_url='http://ml7.stuycs.org:6007'
# This makes the url that we should rediret the user to if they
# want to login using google
def build_redirect_url():
url = baseurl%(scope,redirect_url,client_id)
return url
def code_to_access_token(code):
params=urllib.urlencode({'code':code
,'client_id':client_id
,'client_secret':client_secret
,'redirect_uri':"http://ml7.stuycs.org:6007/auth2callback"
,'grant_type':'authorization_code'})
f=urllib.urlopen('https://accounts.google.com/o/oauth2/token' ,params)
result=f.read()
resultdict = json.loads(result)
return resultdict['access_token']
def access_token_to_info(access_token):
f=urllib.urlopen('https://www.googleapis.com/oauth2/v1/userinfo?access_token=%s'%(access_token))
result=f.read()
resultdict=json.loads(result)
return resultdict