Skip to content
Open
10 changes: 5 additions & 5 deletions fusiontables/authorization/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
import urllib

OAUTH_SETTINGS = {
'scope' : "https://www.google.com/fusiontables/api/query",
'request_token_url':"https://www.google.com/accounts/OAuthGetRequestToken",
'authorize_url':'https://www.google.com/accounts/OAuthAuthorizeToken',
'access_token_url':'https://www.google.com/accounts/OAuthGetAccessToken',
'scope' : "https://www.googleapis.com/auth/fusiontables",
'request_token_url':"https://accounts.google.com/o/oauth2/auth",
'authorize_url':'https://accounts.google.com/o/oauth2/auth',
'access_token_url':'https://www.google.com/fusiontables/api/query',
}

class OAuth():

def generateAuthorizationURL(self, consumer_key, consumer_secret, domain, callback_url=None):
Expand Down
43 changes: 18 additions & 25 deletions fusiontables/ftclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,19 @@ def query(self, query, request_type=None):
return self._post(urllib.urlencode({'sql': query}))


class ClientLoginFTClient(FTClient):
class KeyFTClient(FTClient):

def __init__(self, token):
self.auth_token = token
self.request_url = "https://www.google.com/fusiontables/api/query"
def __init__(self, login_auth_token, key, useCvs = False):
self.auth_token = login_auth_token
self.key = key
self.request_url = "https://www.googleapis.com/fusiontables/v1/query"
self.csv = "&alt=csv" if useCsv else ""

def _get(self, query):
headers = {
'Authorization': 'GoogleLogin auth=' + self.auth_token,
}
serv_req = urllib2.Request(url="%s?%s" % (self.request_url, query),
serv_req = urllib2.Request(url="%s?%s&key=%s%s" % (self.request_url, query, self.key, self.csv),
headers=headers)
serv_resp = urllib2.urlopen(serv_req)
return serv_resp.read()
Expand All @@ -60,36 +62,27 @@ def _post(self, query):
'Content-Type': 'application/x-www-form-urlencoded',
}

serv_req = urllib2.Request(url=self.request_url, data=query, headers=headers)
serv_req = urllib2.Request(url="%s?key=%s%s" % (self.request_url, self.key, self.csv), data=query, headers=headers)
serv_resp = urllib2.urlopen(serv_req)
return serv_resp.read()


class OAuthFTClient(FTClient):

def __init__(self, consumer_key, consumer_secret, oauth_token, oauth_token_secret):
self.consumer_key = consumer_key
self.consumer_secret = consumer_secret
self.token = oauth2.Token(oauth_token, oauth_token_secret)

self.scope = "https://www.google.com/fusiontables/api/query"

def __init__(self, auth_http_client, useCsv = False):
self.client = auth_http_client
self.csv = "&alt=csv" if useCsv else ""
self.scope = "https://www.googleapis.com/fusiontables/v1/query"

def _get(self, query):
consumer = oauth2.Consumer(self.consumer_key, self.consumer_secret)
client = oauth2.Client(consumer, self.token)
resp, content = client.request(uri="%s?%s" % (self.scope, query),
method="GET")
resp, content = self.client.request(uri="%s?%s%s" % (self.scope, query, self.csv), method="GET")
if resp['status'] != '200': raise Exception("%s %s" % (resp['status'], content))
return content



def _post(self, query):
consumer = oauth2.Consumer(self.consumer_key, self.consumer_secret)
client = oauth2.Client(consumer, self.token)
resp, content = client.request(uri=self.scope,
method="POST",
body=query)
resp, content = self.client.request(uri="%s?%s%s" % (self.scope, query, self.csv), method="POST", body=query)
if resp['status'] != '200': raise Exception("%s %s" % (resp['status'], content))
return content




46 changes: 30 additions & 16 deletions fusiontables/samples/oauth_example.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,40 @@
'''
Created on Dec 22, 2010
Created on June 2, 2013

@author: kbrisbin
@author: davious
'''

from fusiontables.authorization.oauth import OAuth
from fusiontables.sql.sqlbuilder import SQL
from fusiontables import ftclient
from fusiontables.fileimport.fileimporter import CSVImporter
import httplib2
import os
# easy_install --upgrade google-api-python-client
import apiclient
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.file import Storage


if __name__ == "__main__":
import sys, getpass
consumer_key = sys.argv[1]
consumer_secret = getpass.getpass("Enter your secret: ")

url, token, secret = OAuth().generateAuthorizationURL(consumer_key, consumer_secret, consumer_key)
print "Visit this URL in a browser: ", url
raw_input("Hit enter after authorization")

token, secret = OAuth().authorize(consumer_key, consumer_secret, token, secret)
oauth_client = ftclient.OAuthFTClient(consumer_key, consumer_secret, token, secret)
client_id = sys.argv[1]
redirect_uri = sys.argv[2]
client_secret = getpass.getpass("Enter your secret: ")
# https://developers.google.com/api-client-library/python/guide/aaa_oauth#storage
flow = OAuth2WebServerFlow(client_id=client_id,
client_secret=client_secret,
scope='https://www.googleapis.com/auth/fusiontables',
redirect_uri=redirect_uri)
storage = Storage(os.path.expanduser("~/oauth_storage.json"))
credentials = storage.get()
if not credentials:
auth_uri = flow.step1_get_authorize_url()
print "Visit this URL in a browser: ", auth_uri
code = raw_input("Enter code appended to the redirect url: ")
credentials = flow.step2_exchange(code)
storage.put(credentials)

http = httplib2.Http()
http.disable_ssl_certificate_validation = True
http = credentials.authorize(http)
oauth_client = OAuthFTClient(http, True)

#show tables
results = oauth_client.query(SQL().showTables())
Expand Down Expand Up @@ -52,4 +66,4 @@

#drop table
print oauth_client.query(SQL().dropTable(tableid))


16 changes: 8 additions & 8 deletions fusiontables/sql/sqlbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def describeTable(self, table_id):
Returns:
the sql statement
"""
return 'DESCRIBE %d' % (table_id)
return 'DESCRIBE %s' % (table_id)

def createTable(self, table):
""" Build a CREATE TABLE sql statement.
Expand Down Expand Up @@ -76,8 +76,8 @@ def select(self, table_id, cols=None, condition=None):
.replace("\'rowid\'", "rowid") \
.replace("\'ROWID\'", "ROWID")

if condition: select = 'SELECT %s FROM %d WHERE %s' % (stringCols, table_id, condition)
else: select = 'SELECT %s FROM %d' % (stringCols, table_id)
if condition: select = 'SELECT %s FROM %s WHERE %s' % (stringCols, table_id, condition)
else: select = 'SELECT %s FROM %s' % (stringCols, table_id)
return select


Expand Down Expand Up @@ -109,7 +109,7 @@ def update(self, table_id, cols, values, row_id):
if count < len(cols): updateStatement = "%s," % (updateStatement)
count += 1

return "UPDATE %d SET %s WHERE ROWID = '%d'" % (table_id, updateStatement, row_id)
return "UPDATE %s SET %s WHERE ROWID = '%d'" % (table_id, updateStatement, row_id)

def delete(self, table_id, row_id):
""" Build DELETE sql statement.
Expand All @@ -121,7 +121,7 @@ def delete(self, table_id, row_id):
Returns:
the sql statement
"""
return "DELETE FROM %d WHERE ROWID = '%d'" % (table_id, row_id)
return "DELETE FROM %s WHERE ROWID = '%d'" % (table_id, row_id)


def insert(self, table_id, values):
Expand Down Expand Up @@ -155,8 +155,8 @@ def insert(self, table_id, values):
if count < len(values): stringValues = "%s," % (stringValues)
count += 1

return 'INSERT INTO %d (%s) VALUES (%s)' % \
(int(table_id), ','.join(["'%s'" % col for col in cols]), stringValues)
return 'INSERT INTO %s (%s) VALUES (%s)' % \
(table_id, ','.join(["'%s'" % col for col in cols]), stringValues)

def dropTable(self, table_id):
""" Build DROP TABLE sql statement.
Expand All @@ -167,7 +167,7 @@ def dropTable(self, table_id):
Returns:
the sql statement
"""
return "DROP TABLE %d" % (table_id)
return "DROP TABLE %s" % (table_id)


if __name__ == '__main__':
Expand Down