From 802e47ecbe2c0a0a27c94af51ffd8806f5f45f1c Mon Sep 17 00:00:00 2001 From: Jeremy Date: Fri, 17 Jun 2016 18:07:19 +0200 Subject: [PATCH] Fix auth with oauth2client. Now requires oauth2client >= 2.0.0. Closes #20 --- .../googlesheets-sheet/connector.json | 2 +- .../googlesheets-sheet/connector.py | 25 +++++++++++-------- googlesheets/requirements.json | 4 +-- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/googlesheets/python-connectors/googlesheets-sheet/connector.json b/googlesheets/python-connectors/googlesheets-sheet/connector.json index d6d344eb..94d05302 100644 --- a/googlesheets/python-connectors/googlesheets-sheet/connector.json +++ b/googlesheets/python-connectors/googlesheets-sheet/connector.json @@ -11,7 +11,7 @@ { "name": "credentials", "label": "Service Account JSON (file provided by Google)", - "type": "STRING", + "type": "TEXTAREA", "mandatory" : true }, { diff --git a/googlesheets/python-connectors/googlesheets-sheet/connector.py b/googlesheets/python-connectors/googlesheets-sheet/connector.py index 5a5985b7..b20e8cc9 100644 --- a/googlesheets/python-connectors/googlesheets-sheet/connector.py +++ b/googlesheets/python-connectors/googlesheets-sheet/connector.py @@ -1,7 +1,8 @@ from dataiku.connector import Connector import json import gspread -from oauth2client.client import SignedJwtAssertionCredentials +import oauth2client +from oauth2client.service_account import ServiceAccountCredentials from slugify import slugify """ @@ -22,15 +23,21 @@ def __init__(self, config): Connector.__init__(self, config) # pass the parameters to the base class # perform some more initialization - credentials = json.loads(self.config.get("credentials")) - self.client_email = credentials["client_email"] - self.private_key = credentials["private_key"] + self.credentials = json.loads(self.config.get("credentials")) self.doc_id = self.config.get("doc_id") self.tab_id = self.config.get("tab_id") self.result_format = self.config.get("result_format") self.list_unique_slugs = [] + def get_spreadsheet(self): + + scope = ['https://spreadsheets.google.com/feeds'] + credentials = ServiceAccountCredentials.from_json_keyfile_dict(self.credentials, scope) + gc = gspread.authorize(credentials) + + return gc.open_by_key(self.doc_id).worksheet(self.tab_id) + def get_unique_slug(self, string): string = slugify(string, to_lower=False,max_length=25,separator="_",capitalize=False) if string == '': @@ -43,6 +50,7 @@ def get_unique_slug(self, string): self.list_unique_slugs.append(test_string) return test_string + def get_read_schema(self): # The Google Spreadsheets connector does not have a fixed schema, since each # sheet has its own (varying) schema. @@ -50,6 +58,7 @@ def get_read_schema(self): # Better let DSS handle this return None + def generate_rows(self, dataset_schema=None, dataset_partitioning=None, partition_id=None, records_limit = -1): """ @@ -60,13 +69,7 @@ def generate_rows(self, dataset_schema=None, dataset_partitioning=None, The dataset schema and partitioning are given for information purpose. """ - - scope = ['https://spreadsheets.google.com/feeds'] - credentials = SignedJwtAssertionCredentials(self.client_email, self.private_key, scope) - gc = gspread.authorize(credentials) - - ws = gc.open_by_key(self.doc_id).worksheet(self.tab_id) - + ws = self.get_spreadsheet() rows = ws.get_all_values() columns = rows[0] columns_slug = map(self.get_unique_slug, columns) diff --git a/googlesheets/requirements.json b/googlesheets/requirements.json index b17d39df..bb4626d7 100644 --- a/googlesheets/requirements.json +++ b/googlesheets/requirements.json @@ -1,7 +1,7 @@ { "python" : [ - {"name": "gspread", "version" : ">=0.2.5"}, - {"name": "oauth2client", "version" : ">=1.5.0"}, + {"name": "gspread", "version" : "==0.3.0"}, + {"name": "oauth2client", "version" : ">=2.0.0,<2.3.0"}, {"name": "PyOpenSSL", "version" : ">=0.15.0"}, {"name": "awesome-slugify", "version" : ">=1.6.5"} ]