From 5d21cc909d53cb2da83f7d0b72265cbdd8880cbd Mon Sep 17 00:00:00 2001 From: davious Date: Wed, 2 Jan 2013 16:46:19 -0500 Subject: [PATCH 01/12] Change sqlbuilder tableid format params from %d to %s --- fusiontables/sql/sqlbuilder.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/fusiontables/sql/sqlbuilder.py b/fusiontables/sql/sqlbuilder.py index cbaf9b8..44a025b 100644 --- a/fusiontables/sql/sqlbuilder.py +++ b/fusiontables/sql/sqlbuilder.py @@ -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. @@ -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 @@ -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. @@ -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): @@ -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. @@ -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__': From 6d846d3424f0a4e65638c4c4c1830a6a6c181c17 Mon Sep 17 00:00:00 2001 From: Dave Anderson Date: Sat, 25 May 2013 22:36:48 -0300 Subject: [PATCH 02/12] Update fusiontables scope to v1 Google has depreciated the old scope; adding the new one. --- fusiontables/ftclient.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fusiontables/ftclient.py b/fusiontables/ftclient.py index 4ef380c..81b1364 100755 --- a/fusiontables/ftclient.py +++ b/fusiontables/ftclient.py @@ -72,7 +72,7 @@ def __init__(self, consumer_key, consumer_secret, oauth_token, oauth_token_secre self.consumer_secret = consumer_secret self.token = oauth2.Token(oauth_token, oauth_token_secret) - self.scope = "https://www.google.com/fusiontables/api/query" + self.scope = "https://www.googleapis.com/fusiontables/v1/query" def _get(self, query): @@ -92,4 +92,4 @@ def _post(self, query): return content - \ No newline at end of file + From f597bcda687c7d51954647de48b000dd67ecc9fd Mon Sep 17 00:00:00 2001 From: Dave Anderson Date: Sat, 25 May 2013 22:40:33 -0300 Subject: [PATCH 03/12] Update fusiontables scope to v1 Google has depreciated old scope; using new one now --- fusiontables/authorization/oauth.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fusiontables/authorization/oauth.py b/fusiontables/authorization/oauth.py index 54b600c..271a25f 100644 --- a/fusiontables/authorization/oauth.py +++ b/fusiontables/authorization/oauth.py @@ -20,7 +20,7 @@ import urllib OAUTH_SETTINGS = { - 'scope' : "https://www.google.com/fusiontables/api/query", + 'scope' : "https://www.googleapis.com/fusiontables/v1/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', From eb6910c1f0924c58da9da264775125d72e0cc5bc Mon Sep 17 00:00:00 2001 From: Dave Anderson Date: Sat, 25 May 2013 23:01:53 -0300 Subject: [PATCH 04/12] Change ClientLoginFTClient to KeyFTClient Google now only supports Google API Key and OAuth access --- fusiontables/ftclient.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/fusiontables/ftclient.py b/fusiontables/ftclient.py index 81b1364..2be36f5 100755 --- a/fusiontables/ftclient.py +++ b/fusiontables/ftclient.py @@ -39,28 +39,26 @@ 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, key, useCvs = False): + 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), + headers = {} + 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() def _post(self, query): headers = { - 'Authorization': 'GoogleLogin auth=' + self.auth_token, '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() From 5e5a573b665d1db87c8502d2e42759b23f52155a Mon Sep 17 00:00:00 2001 From: Dave Anderson Date: Sat, 25 May 2013 23:29:41 -0300 Subject: [PATCH 05/12] Change ClientLoginFTClient to KeyFTClient: Still needs login support? --- fusiontables/ftclient.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/fusiontables/ftclient.py b/fusiontables/ftclient.py index 2be36f5..7627f7f 100755 --- a/fusiontables/ftclient.py +++ b/fusiontables/ftclient.py @@ -41,13 +41,16 @@ def query(self, query, request_type=None): class KeyFTClient(FTClient): - def __init__(self, key, useCvs = False): + 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 "" + self.csv = "&alt=csv" if useCsv else "" def _get(self, query): - headers = {} + headers = { + 'Authorization': 'GoogleLogin auth=' + self.auth_token, + } 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) @@ -55,6 +58,7 @@ def _get(self, query): def _post(self, query): headers = { + 'Authorization': 'GoogleLogin auth=' + self.auth_token, 'Content-Type': 'application/x-www-form-urlencoded', } From 84e44ceb97373f3a714141a839d6224cc24611a2 Mon Sep 17 00:00:00 2001 From: Dave Anderson Date: Sat, 25 May 2013 22:44:54 -0400 Subject: [PATCH 06/12] add csv param for oauth --- fusiontables/ftclient.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/fusiontables/ftclient.py b/fusiontables/ftclient.py index 7627f7f..7ac0191 100755 --- a/fusiontables/ftclient.py +++ b/fusiontables/ftclient.py @@ -69,10 +69,11 @@ def _post(self, query): class OAuthFTClient(FTClient): - def __init__(self, consumer_key, consumer_secret, oauth_token, oauth_token_secret): + def __init__(self, consumer_key, consumer_secret, oauth_token, oauth_token_secret, useCvs = False): self.consumer_key = consumer_key self.consumer_secret = consumer_secret self.token = oauth2.Token(oauth_token, oauth_token_secret) + self.csv = "&alt=csv" if useCsv else "" self.scope = "https://www.googleapis.com/fusiontables/v1/query" @@ -80,7 +81,7 @@ def __init__(self, consumer_key, consumer_secret, oauth_token, oauth_token_secre 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), + resp, content = client.request(uri="%s?%s%s" % (self.scope, query, self.csv), method="GET") return content @@ -88,7 +89,7 @@ def _get(self, query): 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, + resp, content = client.request(uri=self.scope + self.csv.replace("&", "?"), method="POST", body=query) return content From 3d08c4df827d7d47ac92e1cebe97a6c537d4c73e Mon Sep 17 00:00:00 2001 From: Dave Anderson Date: Sat, 1 Jun 2013 21:14:09 -0300 Subject: [PATCH 07/12] Fix required sql param on post issue, added Exception on status != 200 --- fusiontables/ftclient.py | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/fusiontables/ftclient.py b/fusiontables/ftclient.py index 7ac0191..805d572 100755 --- a/fusiontables/ftclient.py +++ b/fusiontables/ftclient.py @@ -74,25 +74,17 @@ def __init__(self, consumer_key, consumer_secret, oauth_token, oauth_token_secre self.consumer_secret = consumer_secret self.token = oauth2.Token(oauth_token, oauth_token_secret) 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%s" % (self.scope, query, self.csv), - 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 + self.csv.replace("&", "?"), - method="POST", - body=query) - return content + 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 From d962635691d2db478414789180eb70edfc02031d Mon Sep 17 00:00:00 2001 From: Dave Anderson Date: Sun, 2 Jun 2013 20:00:40 -0400 Subject: [PATCH 08/12] update oauth settings to oauth2 --- fusiontables/authorization/oauth.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/fusiontables/authorization/oauth.py b/fusiontables/authorization/oauth.py index 271a25f..807ae78 100644 --- a/fusiontables/authorization/oauth.py +++ b/fusiontables/authorization/oauth.py @@ -20,12 +20,12 @@ import urllib OAUTH_SETTINGS = { - 'scope' : "https://www.googleapis.com/fusiontables/v1/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): From 7196a09e92a6a77d98d7c94dc9388e14fc7c914a Mon Sep 17 00:00:00 2001 From: Dave Anderson Date: Sun, 2 Jun 2013 21:22:31 -0300 Subject: [PATCH 09/12] Update oauth_example.py to use google-api-python-client and credential storage --- fusiontables/samples/oauth_example.py | 44 ++++++++++++++++++--------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/fusiontables/samples/oauth_example.py b/fusiontables/samples/oauth_example.py index 1f4d62d..c079502 100644 --- a/fusiontables/samples/oauth_example.py +++ b/fusiontables/samples/oauth_example.py @@ -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: ") + 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) - 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) + 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()) @@ -52,4 +66,4 @@ #drop table print oauth_client.query(SQL().dropTable(tableid)) - \ No newline at end of file + From 6430253b8e7a6ff96e372f5ba9dd8c0fbf20c354 Mon Sep 17 00:00:00 2001 From: Dave Anderson Date: Sun, 2 Jun 2013 21:27:32 -0300 Subject: [PATCH 10/12] update OAuthFTClient.init --- fusiontables/ftclient.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/fusiontables/ftclient.py b/fusiontables/ftclient.py index 805d572..2f735e6 100755 --- a/fusiontables/ftclient.py +++ b/fusiontables/ftclient.py @@ -69,10 +69,8 @@ def _post(self, query): class OAuthFTClient(FTClient): - def __init__(self, consumer_key, consumer_secret, oauth_token, oauth_token_secret, useCvs = False): - self.consumer_key = consumer_key - self.consumer_secret = consumer_secret - self.token = oauth2.Token(oauth_token, oauth_token_secret) + 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" From f26d5147d0c1182e8004eaabee927d472f332bba Mon Sep 17 00:00:00 2001 From: Dave Anderson Date: Sun, 2 Jun 2013 21:28:27 -0300 Subject: [PATCH 11/12] fix OAuthFTClient white space --- fusiontables/ftclient.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/fusiontables/ftclient.py b/fusiontables/ftclient.py index 2f735e6..6a1d5a7 100755 --- a/fusiontables/ftclient.py +++ b/fusiontables/ftclient.py @@ -75,14 +75,14 @@ def __init__(self, auth_http_client, useCsv = False): self.scope = "https://www.googleapis.com/fusiontables/v1/query" def _get(self, query): - resp, content = self.client.request(uri="%s?%s%s" % (self.scope, query, self.csv), 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): 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 + if resp['status'] != '200': raise Exception("%s %s" % (resp['status'], content)) + return content From 642c737d9ac8a2a23c6a9d5d5ba2431e31f374fb Mon Sep 17 00:00:00 2001 From: Dave Anderson Date: Sun, 2 Jun 2013 20:30:07 -0400 Subject: [PATCH 12/12] fix oauth_example white space --- fusiontables/samples/oauth_example.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/fusiontables/samples/oauth_example.py b/fusiontables/samples/oauth_example.py index c079502..f9f4346 100644 --- a/fusiontables/samples/oauth_example.py +++ b/fusiontables/samples/oauth_example.py @@ -29,12 +29,12 @@ 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) + 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())