Skip to content

Commit

Permalink
Fix auth with oauth2client. Now requires oauth2client >= 2.0.0.
Browse files Browse the repository at this point in the history
Closes #20
  • Loading branch information
jereze committed Jun 17, 2016
1 parent 48215b5 commit 802e47e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
Expand Up @@ -11,7 +11,7 @@
{
"name": "credentials",
"label": "Service Account JSON (file provided by Google)",
"type": "STRING",
"type": "TEXTAREA",
"mandatory" : true
},
{
Expand Down
25 changes: 14 additions & 11 deletions 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

"""
Expand All @@ -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 == '':
Expand All @@ -43,13 +50,15 @@ 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.
#
# Better let DSS handle this
return None


def generate_rows(self, dataset_schema=None, dataset_partitioning=None,
partition_id=None, records_limit = -1):
"""
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions 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"}
]
Expand Down

0 comments on commit 802e47e

Please sign in to comment.