diff --git a/fusiontables/authorization/oauth.py b/fusiontables/authorization/oauth.py index 54b600c..807ae78 100644 --- a/fusiontables/authorization/oauth.py +++ b/fusiontables/authorization/oauth.py @@ -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): diff --git a/fusiontables/ftclient.py b/fusiontables/ftclient.py index 4ef380c..6a1d5a7 100755 --- a/fusiontables/ftclient.py +++ b/fusiontables/ftclient.py @@ -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() @@ -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 - \ No newline at end of file + diff --git a/fusiontables/samples/oauth_example.py b/fusiontables/samples/oauth_example.py index 1f4d62d..f9f4346 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: ") - - 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()) @@ -52,4 +66,4 @@ #drop table print oauth_client.query(SQL().dropTable(tableid)) - \ No newline at end of file + 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__':