Package: ohawf
ohawfInstallpipinstall-UohawfAre 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 adeveloperand the
mangledGoogle
exampleshave you frustrated? Well then,ohawfis for you.How to useimportohawfcreds=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 runcreds= 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 FlowsWhat makes ohawf work is this line:fromgoogle_auth_oauthlib.flowimportInstalledAppFlowThe Old Way: Copy/Paste TokenInstalledAppFlow’srun_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 WebserverThe new way which is now the ohawf default isrun_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 Jupyterthis
way. And if you need to force ohawf to use run_console() for a
server installation, you can do this:importohawfcreds=ohawf.get(cli=True)But then you’ll have to whitelist your email address, and you can only
do that through theGoogle Cloud
Consoleso 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 tohttps://console.cloud.google.comMake 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.jsonUse that file with ohawfOnce you have the Client Secret JSON file, you can call ohawf like this:importohawfcreds=ohawf.get(file="client_secret.json")Adding ScopesThe 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.readonlyIf 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 Servicesfrom apiclient.discovery import buildConnect to Google services by givingbuildany (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 LevinPermission 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 PolicyLast updated: December 31, 2022This app does not collect nor use any of Your Personal data.