Google Colab – Google Drive API的重定向URL – OAuth 2.0

huangapple go评论77阅读模式
英文:

Google Colab - redirect_url for Google Drive API - OAuth 2.0

问题

在Google Colab上,我想要从Google Drive下载特定版本的文件到我的工作空间,但我发现这非常困难(与挂载驱动器的简单程度相比)。

我已经创建了OAuth 2.0客户端ID,添加了“期望的”重定向URL并上传了client_secrets.json到我的工作空间。并尝试使用此脚本下载版本:

import requests
from google_auth_oauthlib.flow import InstalledAppFlow

from google.colab import auth
auth.authenticate_user()

# 设置OAuth流程
flow = InstalledAppFlow.from_client_secrets_file('client_secret.json', scopes=['https://www.googleapis.com/auth/drive'])

# 启动OAuth授权过程
credentials = flow.run_local_server(host='localhost',
    port=8081, 
    authorization_prompt_message='请访问此链接:{url}', 
    success_message='授权流程已完成;您可以关闭此窗口。',
    open_browser=True)

# 将凭据保存到文件
credentials.save_to_disk('credentials.json')

# 发送HTTP GET请求到selfLink URL
# 我能够从Google Drive界面上提供的下载链接以及developers.google.com/drive/api/reference/rest/v3中找到FILE_ID和REVISION_ID
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})

# 将响应内容保存为下载的文件
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工作空间保存我的重定向URL,省略了端口,尝试了不同的端口,删除了http,删除了尾部斜杠,在代码中使用了postmessage作为主机(OAuth管理器中没有注册postmessage,因为它需要一个URI)。

我还尝试了像这样使用Google API,在这种情况下,我得到了“文件未找到”的错误:

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工作空间中下载Drive文件的版本吗?
P.S:作为一种解决方法,我无法手动删除修订版本,直到我想要的修订版本,可能是由于Drive一侧的存储约束。这可能会成为另一个问题的主题。

英文:

On Google Colab, I want to download a specific revision of a file from Google Drive to my workspace, however I found it extremely difficult (comparing how easy is to mount the drive).

I have created OAuth 2.0 Client ID, added 'expected' redirect_url's and uploaded the client_secrets.json to my workspace. And trying to download the revision with this script:

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)

However, on flow.run_local_server step, when I visit the authentication link, I'm getting Error 400: redirect_uri_mismatch, where the request detail is:
redirect_uri=http://localhost:8081/.

I have no idea how to make this work, I have waited from 5 mins to hours to let Google workspace to save my redirect url, left out the ports, tried different ports, removed http, removed trailing backslash, used postmessage in the code for host (there is no way to register postmessage in the OAuth manager as it expects a URI).

I have also tried to use the Google API like this, and in this case, I am getting file not found error:

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

I have followed this suggested material as well:
https://github.com/googleapis/google-api-python-client/blob/main/docs/oauth-installed.md

Anybody has an idea (or a workaround) to download a revision of a file from Drive into Google Colab workspace?
P.S: As a workaround, I am not able to delete the revisions manually until the revision I want, probably due to storage constraints on Drive side. This will be a topic of another question I think.

答案1

得分: 3

在Google Cloud控制台中,为您的项目添加以下内容作为重定向URI:

http://localhost:8081/

英文:

in google cloud console for your project add the following as a redirect URI

>http://localhost:8081/

huangapple
  • 本文由 发表于 2023年5月14日 18:11:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76246908.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定