如何在Databricks Python Notebook中输入googleapiclient授权码

How to input googleapiclient authorization code in Databricks Python Notebook

我在 Databricks notebook 中编写了以下代码(只是其中有问题的部分):

    # 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

我 运行 它会将我重定向到 google 登录页面,我执行登录,获取授权码,然后如您所见,我被要求 'Enter the authorization code: '. 问题是我不能entry/input。在我的 Jupiter 笔记本中,它让我有可能在单元格的控制台输出中输入授权码。

但是,使用 Databricks notebook 时,似乎一直在等待代码输入。输出如下:

<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)

所以我找到了解决办法。 首先,我仍然不知道如何在 Databricks 中进行输入,但我已经找到了如何避免执行它。

您可以只打开其他平台,例如 Jupiter notebook,运行 那里有所有代码,包括以下代码:

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())

复制输出后,使用此输出创建一个 json 文件并调用它,例如“token.json”。它必须类似于下图:

    {
     "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"
}

然后将“token.json”上传到Databricks Workspace,然后在其中写入以下代码:

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)

现在应该可以了。它对我有用,但我不会假装非常准确。有任何问题或纠正我。