Skip to main content

Easy Google login from Jupyter Notebooks

Project description

ohawf

Install

pip install -U ohawf

Are you trying to log into Google Analytics, Search Console, or even Google Photos from a Jupyter Notebook? Does all the advice telling you to register as a developer and the mangled Google examples have you frustrated? Well then, ohawf is for you.

How to use

import ohawf

creds = ohawf.get()

Why So Easy?

Google OAuth2 woes go away once you can use the authentication scheme that pops up the same “login as Google” web-prompt that all Mobile and Web apps seem to be using these days. It doesn’t seem possible, but this authentication scheme actually works from a Jupyter Notebook in JupyterLab.

Ohawf makes JupyterLab behave like an installed app, so that when you first run creds= ohawf.get(), you’ll be presented with the “Login with Google” prompt. Just just wait a moment, it will pop up a window separate from JupyterLab’s own, and let you log in then prompt you to close the window. If not, Drink Me.

A Tale of 2 Installed App Flows

What makes ohawf work is this line:

from google_auth_oauthlib.flow import InstalledAppFlow

The Old Way: Copy/Paste Token

InstalledAppFlow’s run_console() prompts you to copy/paste a token from a browser tab to Jupyter and used to work well, but that approach has been deprecated. It can still be forced to work, but it’s not so easy.

The New Way: Gotta Have a Webserver

The new way which is now the ohawf default is run_local_server(). This is what you encounter all the time with mobile apps when you see “Login with Google”. An embedded browser pops up, goes through login, then returns you to the mobile app. This works in Jupyter too. If you have problems, run Jupyter this way. And if you need to force ohawf to use run_console() for a server installation, you can do this:

import ohawf
creds = ohawf.get(cli=True)

But then you’ll have to whitelist your email address, and you can only do that through the Google Cloud Console so chicken-and-egg. If you have to go that route, consider just registering as a Google developer and downloading a OAuth Client secret json file like this:

  • Go to https://console.cloud.google.com
  • Make sure you’re in the correct Google account.
  • Create a new Project.
  • Go to API & Services.
  • Enable the APIs & Services you want to use.
  • Go to Credentials.
  • Create a new OAuth 2.0 Client IDs of the Desktop App type.
  • Go to OAuth consent screen and set it up.
  • Go to +Add users, under test users (gets around tight security)
  • Add the users for the test (your gmail, necessary even though already the app owner)
  • Go back to Credentials and download OAuth Client.
    • Typically, client_secret_[secret].apps.googleusercontent.com.json
  • Use that file with ohawf

Once you have the Client Secret JSON file, you can call ohawf like this:

import ohawf

creds = ohawf.get(file="client_secret.json")

Adding Scopes

The ohawf package uses the following default scopes if you don’t set any:

https://www.googleapis.com/auth/spreadsheets
https://www.googleapis.com/auth/gmail.modify
https://www.googleapis.com/auth/userinfo.email
https://www.googleapis.com/auth/youtube.readonly
https://www.googleapis.com/auth/analytics.readonly
https://www.googleapis.com/auth/webmasters.readonly
https://www.googleapis.com/auth/yt-analytics.readonly
https://www.googleapis.com/auth/photoslibrary.readonly

If you want to set your own scopes, create a Python list of scopes and feed it to ohawf:

scopes = ["https://www.googleapis.com/auth/spreadsheets"]
creds = ohawf(scopes=scopes)

Google Services

from apiclient.discovery import build

Connect to Google services by giving build any (activated) API name, version and credentials such listing your GSC sites:

gsc_service = build('searchconsole', 'v1', credentials=cred)
gsc_sites = gsc_service.sites().list().execute()
[print(x['siteUrl']) for x in gsc_sites['siteEntry']];

…or this go list your GA accounts:

ga_service = build('analytics', 'v3', credentials=cred)
ga_accounts = ga_service.management().accounts().list().execute()
[print((x['id'], x['name'])) for x in ga_accounts['items']];

Copyright (c) 2023 Mike Levin

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Privacy Policy

Last updated: December 31, 2022

This app does not collect nor use any of Your Personal data.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ohawf-23.0.10.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

ohawf-23.0.10-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file ohawf-23.0.10.tar.gz.

File metadata

  • Download URL: ohawf-23.0.10.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for ohawf-23.0.10.tar.gz
Algorithm Hash digest
SHA256 0657f721246f8963efe780d903008c8a2d849b2e1c0841d1ba305193e770f2b6
MD5 25b7ef7c8faff0a4c4f7001dd1b27ae3
BLAKE2b-256 f0c736df5283257ba259e52d285ab4b66b6199227b49af56ca2b3583cb690455

See more details on using hashes here.

File details

Details for the file ohawf-23.0.10-py3-none-any.whl.

File metadata

  • Download URL: ohawf-23.0.10-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for ohawf-23.0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 78d7113c51c2391205309e40b3445835d90cece0e118f10d6c083b8771c877eb
MD5 8f2a4eb3a150d9d7632b5a833eb60c94
BLAKE2b-256 aa6ba84dfa962d5c39369930e197e5af3615a0d81c23794fde89d47f71afb8da

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page