Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Login in Google Colab with Oauth2

I'm trying to login in a Google Colab notebook with Oauth 2.0 but the Google login web page that is launched ends in a non-existent web (http://localhost:8090/?code=4/NAFDWUxkOxp3FIlB9I_vxFTFm-zjAx0XJpEspQ9dGozG-0L3ccthFD12FAhq_B5hLWTxDFuUg_SjC011V9jiLDw&scope=https://www.googleapis.com/auth/admin.directory.group.member%20https://www.googleapis.com/auth/admin.directory.group%20https://www.googleapis.com/auth/apps.groups.settings).

Maybe the error is related with any callback url, but I don't know how to fix that.

This is the code I used for the login.

import os
os.environ['USE_NATIVE_IPYTHON_SYSTEM_COMMANDS'] = '1'

import httplib2

from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import run_flow
from oauth2client import tools

from google.colab import drive; drive.mount('/content/drive')

PATH_BASE = '/content/drive/My Drive/.../'
CLIENT_SECRET = PATH_BASE + 'client_secret_event.json'
SCOPES = [ "https://www.googleapis.com/auth/admin.directory.group", "https://www.googleapis.com/auth/admin.directory.group.member", "https://www.googleapis.com/auth/apps.groups.settings"]
STORAGE = Storage(PATH_BASE + 'credentials.storage')
flags = tools.argparser.parse_args([])

# Start the OAuth flow to retrieve credentials
def authorize_credentials():
    # Fetch credentials from storage
    credentials = STORAGE.get()
    # If the credentials doesn't exist in the storage location then run the flow
    if credentials is None or credentials.invalid:
        flow = flow_from_clientsecrets(CLIENT_SECRET, scope=SCOPES)
        http = httplib2.Http()
        credentials = run_flow(flow, STORAGE, flags, http=http)
    return credentials

credentials = authorize_credentials()

Any idea about why this happens?

like image 249
asabater Avatar asked Oct 14 '25 03:10

asabater


1 Answers

There is an example of how using InstalledAppFlow to use Google Docs with python :

from google_auth_oauthlib.flow import InstalledAppFlow

Useful links:

Using OAuth 2.0 for Installed Applications (with full example)

Sample Using Google Docs

like image 145
Geoffroy de Viaris Avatar answered Oct 16 '25 17:10

Geoffroy de Viaris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!