I have been using a python script which I run from Google Colab to generate documents for about a year now. Nothing was changed to my code (and I double checked this by testing an older version of the program which gave me the same problem) but since the last few days I keep getting an error that I can't resolve.
This is the relevant part of the program:
from __future__ import print_function
import sys
import os.path
from datetime import date, timedelta
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from google_auth_oauthlib.flow import InstalledAppFlow
import googleapiclient.discovery as discovery
from httplib2 import Http
import google.auth
import datetime as datetime
from dateutil.relativedelta import *
import pytz
# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/documents',
'https://www.googleapis.com/auth/contacts',
'https://www.googleapis.com/auth/spreadsheets']
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'/content/drive/MyDrive/geneeskunde/The_administrator/credentials.json', SCOPES)
auth_uri = flow.authorization_url()
# Redirect the user to auth_uri on your platform.
#creds = flow.run_local_server(port=0)
#The following line allowed me to authenticate through Google Colab:
creds = flow.run_console()
# Save the credentials for the next run
with open('token.json', 'w') as token:
token.write(creds.to_json())
#Build services required for the google API's:
peopleservice = build('people', 'v1', credentials=creds)
driveservice = build('drive', 'v3', credentials=creds)
docservice = build('docs', 'v1', credentials=creds)
sheetservice = build('sheets', 'v4', credentials=creds)
The error I get in Google Colab is the following:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-39-8d35889d45f8> in <cell line: 211>()
220
221 #creds = flow.run_local_server(port=0)
--> 222 creds = flow.run_console()
223 # Save the credentials for the next run
224 with open('token.json', 'w') as token:
AttributeError: 'InstalledAppFlow' object has no attribute 'run_console'
I tried to solve this issue myself and on 【Google OAuth】AttributeError: 'InstalledAppFlow' object has no attribute 'run_console' describe exactly this problem with the following solution:
Expanding on George's answer above, the versions of the library from the github post that work are:
google-api-python-client==1.7.2
google-auth==1.8.0
google-auth-httplib2==0.0.3
google-auth-oauthlib==0.4.1
I tried installing these versions on my colab session using
!pip install google-api-python-client==1.7.2
!pip install google-auth==1.8.0
...
Unfortunately the same error reappeared.
I found extra info on the run_console function on this site: https://google-auth-oauthlib.readthedocs.io/en/latest/reference/google_auth_oauthlib.flow.html
But I can't seem to fix this issue.
Any help or insight would be most appreciated.
Regards,
Usui