在Google CoLab Notebook中,如何从公共Google云端硬盘和我的个人硬盘中读取数据,而无需进行两次身份验证?

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

采用另一种方式:如果我已经通过身份验证并验证了谁可以挂载驱动器,那么为什么我需要再次做一次才能从公开共享的Google云端硬盘中读取数据?

我认为在第二种方法中会以某种方式从一种方法的第一步使用身份验证(请参见下面的详细信息),或者以某种方式在一个步骤中请求对两者的权限,但是我没有运气来解决它。

背景

关于如何将数据读取到Google colab笔记本中的文章很多:Import data into Google ColaboratoryTowards Data Science - 3 ways to load CSV files into colabGoogle CoLab's official helper notebook是一些很好的参考。

要快速回顾一下,您可以根据数据的来源选择几种方法。如果您要使用自己的数据,那么一个简单的解决方案是将数据放入Google云端硬盘,然后挂载驱动器。

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

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

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

在这种情况下,您可以使用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帐户身份:复制一个代码,然后将代码粘贴回笔记本中。

在Google CoLab Notebook中,如何从公共Google云端硬盘和我的个人硬盘中读取数据,而无需进行两次身份验证?

这是我的问题:

我想在同一笔记本上同时做这两项事情:(1)从已安装的Google驱动器中读取,以及(2)从公共共享的GDrive中读取。 我的笔记本电脑的用户是第三方。如果笔记本计算机同时运行这两组代码,则将迫使用户执行两次身份验证验证代码。这是一个糟糕的UX,令人困惑,似乎应该没有必要。

我尝试过的事情

关于此代码:

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()行,我的第三方用户就会收到以下错误:

   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。

在Google CoLab Notebook中,如何从公共Google云端硬盘和我的个人硬盘中读取数据,而无需进行两次身份验证?

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

在Google CoLab Notebook中,如何从公共Google云端硬盘和我的个人硬盘中读取数据,而无需进行两次身份验证?

所以这些是不同的权限。

所以,问题是...是否有任何方法可以简化此流程并将所有这些权限打包到单个验证码身份验证工作流程中?如果我想同时读取已安装的Drive和公共共享的GDrive,是否需要笔记本用户进行双重身份验证?

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

tomehack110 回答:在Google CoLab Notebook中,如何从公共Google云端硬盘和我的个人硬盘中读取数据,而无需进行两次身份验证?

没有办法做到这一点。 OAuth范围有所不同,一个是针对 Google云端硬盘文件系统的;另一个用于 Google Cloud SDK

本文链接:https://www.f2er.com/3163019.html

大家都在问