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

发布于 01-23 15:50 字数 1273 浏览 5 评论 0原文

我已经在Databricks笔记本中写下了以下代码(只是它的问题部分):

    # 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登录页面,我执行登录,获取授权代码,然后您被问到我被问到要“输入授权代码:”。 问题是i 无法进入条目/输入。在我的木星笔记本电脑中,它使我有可能仅在单元格输出中输入验证代码。

但是,使用Databricks笔记本电脑,它似乎一直在等待代码输入。输出如下:

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

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

誰ツ都不明白2025-01-30 15:50:19

所以我找到了解决方案。
首先,我仍然不知道如何在Databricks中进行输入,但是我发现了如何避免执行它。

您只需打开其他平台,例如木星笔记本,在此处运行所有代码,包括下面的代码:

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)

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文