使用远程笔记本从驱动器下载文件

时间:2021-02-11 08:18:35

标签: python-3.x oauth jupyter-notebook google-drive-api client-server

我不太熟悉网络概念,所以我希望能得到一点帮助。基本上,我正在尝试将我的笔记本(由 Paperspace 托管在 Gradient 上)连接到我的 Google Drive。目标是下载大量训练文件数据集。

我的 OAuth 已全部设置完毕,我已将凭据.json 上传到 Gradient。我可以获得 Google 的身份验证链接,单击该链接会将我带到身份验证屏幕,在那里我可以授予对脚本的访问权限。一切正常,直到这里。在我授予所需的权限后,发生错误。页面被重定向到本地主机,我看到页面加载失败,而不是成功消息。确切的消息是“本地主机拒绝连接”。我确定这是因为 OAuth 正在尝试向我的本地机器发送响应。但是我如何将它路由到我的笔记本正在运行的 Gradient。我已经厌倦了以下内容:

  1. 更改 jupyter 配置文件以设置 origins = " * "
  2. NotebookApp.ip 更改为 " * "
  3. jupyter notebook list 并得到 Currently running servers: http://0.0.0.0:8888/?token=XXXXXX :: /notebooks。然后我在下面的代码中使用了相同的端口 (8888)。

然而,到目前为止似乎没有任何效果。我怀疑 self.creds = flow.run_local_server(port=8888) 可能是罪魁祸首。但是我找不到任何方法来启动远程服务器。我仍然看到无法显示页面的相同错误。任何人都可以帮助我连接到我的云端硬盘文件夹。更重要的是,我如何概括这些概念,以便我可以将它们用于任何服务器-客户端身份验证?

from google.auth.transport.requests import Request
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
 
 
class Auth:
 
    def __init__(self, client_secret_filename, scopes):
        self.client_secret = client_secret_filename
        self.scopes = scopes
        self.flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(self.client_secret, self.scopes)
        self.flow.redirect_uri = 'https://{address to gradeient notebook}/notebooks/storage/data/scrp.ipynb'
        self.creds = None
 
    def get_credentials(self):
        flow = InstalledAppFlow.from_client_secrets_file(self.client_secret, self.scopes)
        self.creds = flow.run_local_server(port=8888)
        return self.creds

 
# The scope you app will use. 
# (NEEDS to be among the enabled in your OAuth consent screen)
SCOPES = "https://www.googleapis.com/auth/drive.readonly"
CLIENT_SECRET_FILE = "credentials.json"
 
credentials = Auth(client_secret_filename=path, scopes=SCOPES).get_credentials()
 
drive_service = build('drive', 'v3', credentials=credentials)````

0 个答案:

没有答案