How to input googleapiclient authorization code in Databricks Python Notebook

Viewed 150

I have written the following code in Databricks notebook (just the problematic part of it):

    # Connection
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
    client_secrets_file, scopes)

print(flow)
credentials = flow.run_console()

youtube_analytics = googleapiclient.discovery.build(
        api_service_name, api_version, credentials=credentials)
print(youtube_analytics)

# After this password would ask you to visit the url to authorize this application 
# Youtube Project wants to access your Google account

I run it, it redirects me to google sign in page, I perform a sign in, get an authorization code, and as you see I am then asked to 'Enter the authorization code: '. The problem is I can't make the entry/input. In my Jupiter notebook it gives me a possibility to enter the auth code just in console output of the cell.

However, with Databricks notebook, it appears to be constantly waiting for the code input. Output is below:

<google_auth_oauthlib.flow.InstalledAppFlow object at 0x7f3b921665b0>
Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=123456789012-2jk0lelmg5m4p5s6c0b284potst9cumd.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyt-analytics.readonly&state=EBizXejyThoJCSszfypDsqczBSN2Tk&prompt=consent&access_type=offline
Enter the authorization code: (*here I can't input the code)
1 Answers

So I have found a solution. Firstly, I still do not know how to make an input in Databricks, but I have found how to avoid performing it.

You can just open some other platform, for example Jupiter notebook, run there all the code including the one below:

import os
import google_auth_oauthlib.flow
import googleapiclient.errors
from googleapiclient import discovery

# Disable OAuthlib's HTTPS verification when running locally.
# *DO NOT* leave this option enabled in production.
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

# From Google Console 
client_secrets_file = 'client_secrets.json' # YOUR FILE NAME
scopes = ['https://www.googleapis.com/auth/yt-analytics.readonly']
api_service_name = "youtubeAnalytics"
api_version = "v2"

flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
    client_secrets_file, scopes)
    
print(flow)
credentials = flow.run_console()
    
print(credentials.to_json())

After this copy the output, create a json file with this output and call it for example "token.json". It must look something like the one below:

    {
     "token": "your_token",
     "refresh_token": "your_refresh_token",
     "token_uri": "your_token_uri",
     "client_id": "your_cli_id",
     "client_secret": "your_cli_secret",
     "scopes": ["your_scopes"],
     "expiry": "your_expiry_date"
}

Then upload "token.json" to Databricks Workspace and then write the following code there:

import os
import google_auth_oauthlib.flow
import googleapiclient.errors
from googleapiclient import discovery
from google.oauth2.credentials import Credentials

# Disable OAuthlib's HTTPS verification when running locally.
# *DO NOT* leave this option enabled in production.
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

# From Google Console 
client_secrets_file = "client_secrets.json" # YOUR FILE NAME
scopes = ['https://www.googleapis.com/auth/yt-analytics.readonly']
api_service_name = "youtubeAnalytics"
api_version = "v2"

creds = Credentials.from_authorized_user_file(
        "token.json", scopes)
if not creds or not creds.valid:
    if creds and creds.expired and creds.refresh_token:
        creds.refresh(Request())
    else:
        flow = InstalledAppFlow.from_client_secrets_file(
            client_secrets_file, scopes) #enter your scopes and secrets file
        creds = flow.run_local_server(port=0)
    # Save the credentials for the next run
    with open('token.json', 'w') as token:
        token.write(creds.to_json())
    
youtube_analytics = googleapiclient.discovery.build(
        api_service_name, api_version, credentials=creds)

And now it should work. It works for me, but I do not pretend to be very accurate. Ask any questions or correct me.

Related