Skip to main content

The easiest Google OAuth2 you ever did see.

Project description

ohawf

Google OAuth2 login that's less oh awf ul

Install

pip install -U ohawf

Google services require OAuth2 login which is often hard for newbs. If you're trying to do Python things with your Google data (GA, GSC, Sheets, Photos, etc.) and have failed in the past over login issues (should be the easy part but is not), ohawf is for you. It simplifies processes like GSC API quickstart for example.

How to use

import ohawf
cred = ohawf.get()

If it is your first time running you will get a link for the Web-based Google OAuth2 login prompt that looks like:

Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=[foo]&redirect_uri=[foo]&state=[foo]&prompt=consent&access_type=offline
Enter the authorization code:_________________________

Click it and from the familiar Google login prompt that pops-up pick the account you want to use. If you get a security warning you have to click Advanced and Go to OhAwf (unsafe). If this bothers you, you can optionally create your own credentials.json file as described below. Copy/paste the resulting text (a.k.a. "token") back into the Jupyter field and hit Enter. You will then have the authenticated credentials object needed to connect to and interact with Google services.

Running from Terminal

If you are running from a command-line Terminal you will have to copy/paste the login link from your Terminal into a browser, login as normal, and copy the token back into the Terminal. This generally only ever has to be done once.

Security

This process deposits a credentials.pkl file in that folder that will be continually refreshed eliminating the need for you to have to log in again. Keep both your credentials.json and credentials.pkl file secret. In other words, do not commit them to your Github repos. To help from doing it accidentally, add them both to your .gitignore file.

Custom Credentials File

By default, the credentials.json file will be used. It is unverified by Google and will cause an OAuth2 security warning on the prompt. You may have to click Advanced and Proceed Anyway If you wish to provide your own credentials.json file to avoid this warning (optional), follow the below process.

  • Go to https://console.cloud.google.com/
  • 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.
  • Go to +Add users, under test users.
  • Add the users for the test (your gmail, necessary even though already the app owner)
  • Download that credentials file.
  • Rename that file to default.json
  • Drop that file into your working directory

Rename it to "credentials.json" and drop it into your working folder and it will use it instead of the provided credentials.json.

Adding Scopes

If you want to work with more Google services than just Analytics and Search Console (there are hundreds) then activate them and add their scope URLs to the scopes.csv file, one per line.

Google Services

After we have an authenticated credentials object, we build Google services objects with build from the Google API Python client installed as a requirement of ohawf.

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('webmasters', 'v3', 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']];

Notice how clean this code is compared to GSC API quickstart. With better security (OAuth2) comes more complicated procedures. Ohawf is the most lightweight way I have found to alleviate this.

About the Author

I used to be Mike Levin, SEO in NYC. Since our lovely pandemic I got my butt back to Pennsylvania and am now the Poconos Pythonista focusing on the kind of data exploration and automation folks are calling Data Engineering these days. Once upon a time, I worked for legendary Commodore Computers due to my love for the amazing Amiga computer, which spoiled me then caused me to be disappointed with everything to follow. Finally I'm finding the love again using a certain mix of Linux, Python, vim and git (plus Jupyter and Virtual Desktops). Today, I'm part of the J2 family of sites including Mashable, PCMag, Everyday Health and RetailMeNot. To thank me for this package, visit these sites. In particular if you use browser shopping plugins and mobile apps, try RMN's coupon app or coupon plugin.

Copyright (c) 2021 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: March 10, 2021

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-0.1.4.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ohawf-0.1.4-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for ohawf-0.1.4.tar.gz
Algorithm Hash digest
SHA256 0a49c05aa2fc02c16e6d201968cda7bd6506a96f3d9992d24bfe6644091e6588
MD5 6084221763db78e43840a8d30f479674
BLAKE2b-256 786db82c87b3a4831209047eb862480c2a8d6e5a14240673465e7f6709e19319

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for ohawf-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 17464f931c270bc8e36d1cefc8f61c0c567565d2a66648c1ecb9266212fcf337
MD5 b56dbdc5df2243713a4232752f23a4e4
BLAKE2b-256 f6330178471491f82ad91e5de9bff4ea5bb4eb8d17403275d403d255fb5a0f2d

See more details on using hashes here.

Supported by

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