From ad3cf187e241e86ad3bc196570f09b23d04033b0 Mon Sep 17 00:00:00 2001 From: "Nasreddin Boulif (bon)" Date: Wed, 2 Feb 2022 15:53:31 +0000 Subject: [PATCH] [FIX] google_spreadsheet: migrate to new api (v3 -> v4) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Steps to Reproduce: - Install 'account' module - Go to Settings and set a token for Google Drive - Activate Google Spreadsheet - Go to Invoicing -> Invoices - Under Search bar, click on Favorites -> Add to Google Spreadsheet Issue: A spreadsheet does open but with no data (no formula or config). Cause: The V3 API we were using was turned down in August 2021 and therefore not able to use the api to write on the spreadsheet. https://cloud.google.com/blog/products/g-suite/migrate-your-apps-use-latest-sheets-api) Note: In case that the system parameters `​google_drive_client_id` and `google_drive_client_secret` has been changed (and therefore also the default templates), it must be ensured that the `client` has the Google Sheet API (v4) activated and the scope `https://www.googleapis.com/auth/spreadsheets` set. API activation and configuration available here : https://console.developers.google.com/apis/dashboard?project=[PROJECT] https://console.developers.google.com/apis/credentials/consent/edit?project=[PROJECT] **[PROJECT]** : Google project that will be linked to `​google_drive_client_id`. opw-2633951 closes odoo/odoo#84488 X-original-commit: 3268530a071d9351ca86776fe85289ee5feece3d Signed-off-by: Nasreddin Boulif (bon) --- .../google_spreadsheet/models/google_drive.py | 39 ++++++------------- 1 file changed, 12 insertions(+), 27 deletions(-) diff --git a/addons/google_spreadsheet/models/google_drive.py b/addons/google_spreadsheet/models/google_drive.py index c76d0d989ef..10e04bbb050 100644 --- a/addons/google_spreadsheet/models/google_drive.py +++ b/addons/google_spreadsheet/models/google_drive.py @@ -20,11 +20,11 @@ class GoogleDrive(models.Model): def get_google_scope(self): scope = super(GoogleDrive, self).get_google_scope() - return '%s https://spreadsheets.google.com/feeds' % scope + return '%s https://www.googleapis.com/auth/spreadsheets' % scope @api.model def write_config_formula(self, attachment_id, spreadsheet_key, model, domain, groupbys, view_id): - access_token = self.get_access_token(scope='https://spreadsheets.google.com/feeds') + access_token = self.get_access_token(scope='https://www.googleapis.com/auth/spreadsheets') fields = self.env[model].fields_view_get(view_id=view_id, view_type='tree') doc = etree.XML(fields.get('arch')) @@ -50,33 +50,18 @@ class GoogleDrive(models.Model): config_formula = '=oe_settings("%s";"%s")' % (url, dbname) else: config_formula = '=oe_settings("%s";"%s";"%s";"%s")' % (url, dbname, username, password) - request = ''' - https://spreadsheets.google.com/feeds/cells/{key}/od6/private/full - - A1 - - https://spreadsheets.google.com/feeds/cells/{key}/od6/private/full/R1C1 - - - - - A2 - - https://spreadsheets.google.com/feeds/cells/{key}/od6/private/full/R60C15 - - - -''' .format(key=spreadsheet_key, formula=misc.html_escape(formula), config=misc.html_escape(config_formula)) - + request = { + "valueInputOption": "USER_ENTERED", + "data": [ + {"range": "A1", "values": [[formula]]}, + {"range": "O60", "values": [[config_formula]]}, + ] + } try: req = requests.post( - 'https://spreadsheets.google.com/feeds/cells/%s/od6/private/full/batch?%s' % (spreadsheet_key, werkzeug.urls.url_encode({'v': 3, 'access_token': access_token})), - data=request.encode('utf-8'), - headers={'content-type': 'application/atom+xml', 'If-Match': '*'}, + 'https://sheets.googleapis.com/v4/spreadsheets/%s/values:batchUpdate?%s' % (spreadsheet_key, werkzeug.urls.url_encode({'access_token': access_token})), + data=json.dumps(request), + headers={'content-type': 'application/json', 'If-Match': '*'}, timeout=TIMEOUT, ) except IOError: