Authenticate to Google API on a cloud-based notebook

When working with Google APIs on a AI Platform Notebook, you cannot access localhost like on a local machine. To generate credentials, the Google Auth library gives you the option to generate a callback URL on a remote address. For more details, refer to this support page.

# Import library and start authentication flow
from google_auth_oauthlib import flow

appflow = flow.InstalledAppFlow.from_client_secrets_file(

Running a local server

Visit the URL, authorize your app when prompted, and the autentication process will automatically complete.

# If running a local server:
Please visit this URL to authorize this application:

Running a remote server

In this case, for example when running code on an AI Platform Notebook, you cannot use localhost as callback URL. Use run_console() instead, and after visiting the URL and authorizing your app, you will be given an authorization code to be pasted in the prompt.

# If running a remote server:
Please visit this URL to authorize this application:

Enter the authorization code:  4/ywF6LRH5rJoC6f0531aNgoapZqepKtJRBOPAPMzHdF0ToBk4k