Created
February 25, 2012 10:19
-
-
Save a2chub/1907732 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#coding:utf-8 | |
import os, sys | |
import oauth2client | |
import httplib2 | |
from urllib import urlencode | |
import urllib2 | |
from pyquery import PyQuery as pq | |
import requests | |
import json | |
Client_ID = "?????????????????????????????????.apps.googleusercontent.com" | |
Client_secret = "???????????????????????????" | |
get_token_url = "https://accounts.google.com/o/oauth2" | |
base_url = "https://www.googleapis.com/auth/calendar" | |
h = httplib2.Http(".cache") | |
def get_tocken(): | |
global get_token_url | |
resp, content = h.request(get_token_url+'/auth?%s'%(urlencode(get_data())), "GET") | |
print resp | |
if resp['status'] == '200': | |
print content | |
req_url = resp['content-location'] | |
print resp['content-location'] | |
else: | |
req_url = 'http://google.com' | |
for i in resp: | |
print i, '\t\t:', resp[i] | |
if sys.platform == 'darwin': | |
os.popen('open /Applications/Google\ Chrome.app "%s"'%(req_url)) | |
os.popen('pbcopy "%s"&'%(req_url)) | |
authorization_code = raw_input('\n\nWhat your auth code>>> \t') | |
h2 = httplib2.Http(".cache") | |
# urllib2でやってみる | |
r = urllib2.Request( | |
url=get_token_url + '/token', | |
data=urlencode(get_token(authorization_code)) | |
) | |
res = urllib2.urlopen(r) | |
res_token = json.loads(res.read()) | |
res.close() | |
print res_token | |
print res_token['access_token'] | |
return res_token['access_token'] | |
def get_token(auth_code): | |
global Client_ID, Client_secret | |
data_hash = dict( | |
code="%s"%(auth_code), | |
client_id=Client_ID, | |
client_secret=Client_secret, | |
redirect_uri="urn:ietf:wg:oauth:2.0:oob", | |
grant_type="authorization_code" | |
) | |
return data_hash | |
def get_data(): | |
global Client_ID | |
data_hash = dict( | |
response_type="code", | |
client_id=Client_ID, | |
redirect_uri="urn:ietf:wg:oauth:2.0:oob", | |
scope="https://www.googleapis.com/auth/userinfo.profile \ | |
https://www.googleapis.com/auth/userinfo.email", | |
) | |
return data_hash | |
def get_user_info(acc_token): | |
req_url = "https://www.googleapis.com/oauth2/v2/userinfo" | |
headers = {'Authorization': 'OAuth ' + acc_token} | |
r = requests.get(req_url, headers=headers) | |
print dir(r) | |
print r.headers | |
ret = json.loads(r.text) | |
print ret | |
for i in ret: | |
print i, '\t\t', ret[i] | |
if __name__ == '__main__': | |
print '=' * 80 | |
now_token = get_tocken() | |
get_user_info(now_token) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment