Google Colab - Google Drive API 的 redirect_url - OAuth 2.0

问题描述 投票:0回答:0

在 Google Colab 上,我想从 Google Drive 下载文件的特定修订版到我的工作区,但是我发现这非常困难(比较安装驱动器的容易程度)。

我已经创建了 OAuth 2.0 客户端 ID,添加了“预期的”redirect_url,并将 client_secrets.json 上传到我的工作区。并尝试使用此脚本下载修订版:

import requests
from google_auth_oauthlib.flow import InstalledAppFlow

from google.colab import auth
auth.authenticate_user()

# Set up the OAuth flow
flow = InstalledAppFlow.from_client_secrets_file('client_secret.json', scopes=['https://www.googleapis.com/auth/drive'])

# Start the OAuth authorization process
credentials = flow.run_local_server(host='localhost',
    port=8081, 
    authorization_prompt_message='Please visit this URL: {url}', 
    success_message='The auth flow is complete; you may close this window.',
    open_browser=True)

# Save the credentials to a file
credentials.save_to_disk('credentials.json')

# Make an HTTP GET request to the selfLink URL
# I was able to fetch the FILE_ID and REVISION_ID both from download link given on Google Drive's UI
# ...and on developers.google.com/drive/api/reference/rest/v3

self_link = f'https://www.googleapis.com/drive/v2/files/{FILE_ID}/revisions/{REVISION_ID}'
response = requests.get(self_link, headers={'Authorization': 'Bearer ' + credentials.token})

# Save the response content as the downloaded file
with open('your_file_name', 'wb') as file:
    file.write(response.content)

但是,在

flow.run_local_server
步骤中,当我访问身份验证链接时,我得到了
Error 400: redirect_uri_mismatch
,其中请求详细信息是:
redirect_uri=http://localhost:8081/
.

我不知道如何进行这项工作,我已经等了 5 分钟到几个小时让 Google workspace 保存我的重定向 url,省略了端口,尝试了不同的端口,删除了 http,删除了尾部反斜杠,使用了

postmessage
主机代码(无法在 OAuth 管理器中注册邮寄消息,因为它需要 URI)。

我也试过像这样使用谷歌API,在这种情况下,我得到

file not found
错误:

from googleapiclient.discovery import build
drive_service = build('drive', 'v3', credentials=credentials)
files = drive_service.revisions().get_media(fileID=FILE_ID, revisionID=REVISION_ID).execute()

我也遵循了这个建议的材料: https://github.com/googleapis/google-api-python-client/blob/main/docs/oauth-installed.md

有人有想法(或解决方法)将文件的修订版从云端硬盘下载到 Google Colab 工作区吗? P.S:作为一种解决方法,我无法手动删除修订,直到我想要的修订,可能是由于驱动器端的存储限制。我认为这将是另一个问题的主题。

python oauth-2.0 google-drive-api google-oauth google-colaboratory
© www.soinside.com 2019 - 2024. All rights reserved.