首页 > 解决方案 > 在 Google CoLab Notebook 中,如何在不经过两次身份验证的情况下从公共 Google 云端硬盘和我的个人云端硬盘中读取数据?

问题描述

我有一个第三方使用的 Google CoLab 笔记本。笔记本的用户需要笔记本从他们个人安装的 GDrive以及第 3 方公开共享的 GDrive 中读取 CSV。据我所知,从这两个不同的来源读取每个都需要用户完成身份验证验证码工作流程,每次都复制/粘贴代码。如果他们只需要进行一次身份验证验证,而不是 2 次,则 UX 将会大大改善。

换句话说:如果我已经验证并验证了我是谁来安装我的驱动器,那么为什么我需要再次这样做才能从公开共享的 Google Drive 中读取数据?

我想有办法在第二种方法的第一步中使用一种方法的身份验证(请参阅下面的详细信息),或者以某种方式在一个步骤中请求两者的权限,但我没有任何运气弄清楚它。

背景

关于如何将数据读入 Google Colab notebooks 的文章很多:Import data into Google Colaboratory & Towards Data Science - 3 ways to load CSV files into colabGoogle CoLab's official helper notebook是一些很好的参考。

快速回顾一下,您有几个选择,具体取决于数据的来源。如果您使用自己的数据,那么一个简单的解决方案是将您的数据放入 Google Drive,然后安装您的驱动器。

from google.colab import drive as mountGoogleDrive
mountGoogleDrive.mount('/content/mountedDrive')

您可以像在本地文件系统中一样读取文件content/mountedDrive/

有时安装驱动器是不够的。例如,假设您要从第三方拥有的公开共享的 Google 云端硬盘中读取数据。在这种情况下,您无法挂载您的驱动器,因为共享数据不在您的驱动器中。您可以将所有数据从 3rd 方驱动器复制到您的驱动器中,但最好直接从公共驱动器读取,特别是如果这是许多人使用的共享笔记本。

在这种情况下,您可以使用 PyDrive(参见相同的参考资料)。

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

# Authenticate and create the PyDrive client.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

您必须查找数据集的驱动器 ID,然后才能读取它,例如,如下所示:

import pandas as pd
downloaded = drive.CreateFile({'id':id}) 
downloaded.GetContentFile('Filename.csv') 
df = pd.read_csv('Filename.csv') 

在这两个工作流程中,您必须通过访问特殊链接、复制代码并将代码粘贴回笔记本中来验证您的 Google 帐户。

在此处输入图像描述

这是我的问题:

我想在同一个笔记本上做这两件事:(1)从安装的谷歌驱动器读取和(2)从公开共享的 GDrive 读取。我的笔记本的用户是第三方。如果笔记本运行两组代码,则用户被迫执行两次身份验证验证代码。这是一个糟糕的用户体验,令人困惑,而且看起来应该是不必要的。

我尝试过的事情

关于这段代码:

auth.authenticate_user() # We already authenticated when we mounted our GDrive
gauth = GoogleAuth()

我认为可能有一种方法可以将gauth对象传递给.mount()函数,这样如果凭据已经存在,您就不需要使用新的验证码重新请求身份验证。但是我无法找到关于 的文档google.colab.drive.mount(),并且在传递参数时随机猜测是行不通的。

或者,我们可以反之亦然,但是我不确定是否可以从.mount().

接下来我尝试运行以下代码,authenticate_user()在安装完成后删除显式调用,如下所示:

from google.colab import drive as mountGoogleDrive
mountGoogleDrive.mount('/content/mountedDrive')

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

# Authenticate and create the PyDrive client.
# auth.authenticate_user() # Commented out, hoping we already authenticated during mounting
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

前两行按预期运行,包括认证链接和验证码。但是,一旦我们到达线路,gauth.credentials = GoogleCredentials.get_application_default()我的第 3 方用户就会收到以下错误:

   1260         # If no credentials, fail.
-> 1261         raise ApplicationDefaultCredentialsError(ADC_HELP_MSG)
   1262 
   1263     @staticmethod

ApplicationDefaultCredentialsError: The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.

我不是 100% 完成了这些不同的行,所以我也尝试删除错误行:

from google.colab import drive as mountGoogleDrive
mountGoogleDrive.mount('/content/mountedDrive')

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

# Authenticate and create the PyDrive client.
# auth.authenticate_user() # Commented out, hoping we already authenticated during mounting
gauth = GoogleAuth()
# gauth.credentials = GoogleCredentials.get_application_default() # Commented out, hoping we don't need this line if we are already mounted? 
drive = GoogleDrive(gauth)

现在运行没有错误,但是当我尝试从公共驱动器读取文件时,我收到以下错误:

InvalidConfigError: Invalid client secrets file ('Error opening file', 'client_secrets.json', 'No such file or directory', 2)

在这一点上,我注意到一些可能很重要的事情:

当我运行驱动器安装代码时,身份验证请求访问 Google DriveFile Stream。

在此处输入图像描述

当我运行 PyDrive 身份验证时,身份验证代表 Google Cloud SDK 请求访问。

在此处输入图像描述

所以这些是不同的权限。

所以,问题是……有没有办法简化这一点并将所有这些权限打包成一个单一的验证码身份验证工作流程?如果我想从我安装的驱动器和公共共享的 GDrive 中读取,是否需要笔记本用户进行双重身份验证?

感谢您提供任何指向文档或示例的指针。

标签: pythonauthenticationgoogle-drive-apijupyter-notebookgoogle-colaboratory

解决方案


没有办法做到这一点。OAuth 范围不同,一种是针对Google Drive 文件系统;另一个用于Google Cloud SDK


推荐阅读