Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Google CoLab笔记本中,如何在不进行两次*身份验证的情况下从公用Google驱动器和我的个人驱动器*读取数据?_Python_Authentication_Google Drive Api_Jupyter Notebook_Google Colaboratory - Fatal编程技术网

Python 在Google CoLab笔记本中,如何在不进行两次*身份验证的情况下从公用Google驱动器和我的个人驱动器*读取数据?

Python 在Google CoLab笔记本中,如何在不进行两次*身份验证的情况下从公用Google驱动器和我的个人驱动器*读取数据?,python,authentication,google-drive-api,jupyter-notebook,google-colaboratory,Python,Authentication,Google Drive Api,Jupyter Notebook,Google Colaboratory,我有一个第三方使用的Google CoLab笔记本。笔记本用户需要笔记本从其个人安装的GDrive以及第三方公开共享的GDrive读取CSV。 据我所知,从这两个不同的来源读取每一个都需要用户完成一个身份验证码工作流,每次都需要复制/粘贴一个代码。如果用户体验只需要进行一次身份验证,而不是两次,那么用户体验将得到很大的改进 换句话说:如果我已经验证了我是谁来装载我的驱动器,那么为什么我需要再次这样做才能从一个公共共享的谷歌驱动器读取数据 我想在第二个方法的第一个步骤中可以使用一个方法的身份验证(

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

换句话说:如果我已经验证了我是谁来装载我的驱动器,那么为什么我需要再次这样做才能从一个公共共享的谷歌驱动器读取数据

我想在第二个方法的第一个步骤中可以使用一个方法的身份验证(请参阅下面的详细信息),或者在一个步骤中以某种方式请求对这两个方法的权限,但我没有找到解决方法

背景

关于如何将数据读入GoogleColab笔记本的文章很多:& 这是一些很好的参考资料

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)
要快速回顾,您有几个选项,具体取决于数据来自何处。如果您正在处理自己的数据,那么一个简单的解决方案是将数据放入Google Drive,然后装入驱动器

from google.colab import drive as mountGoogleDrive
mountGoogleDrive.mount('/content/mountedDrive')
您可以像读取本地文件系统中的文件一样,在
content/mountedDrive/
读取文件

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

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

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

import pandas as pd
downloaded = drive.CreateFile({'id':id}) 
downloaded.GetContentFile('Filename.csv') 
df = pd.read_csv('Filename.csv') 
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)
在这两种工作流程中,您必须通过以下方式对您的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\u user()
调用,如下所示:

import pandas as pd
downloaded = drive.CreateFile({'id':id}) 
downloaded.GetContentFile('Filename.csv') 
df = pd.read_csv('Filename.csv') 
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\u application\u default()
my第三方用户就会出现以下错误:

   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.
InvalidConfigError: Invalid client secrets file ('Error opening file', 'client_secrets.json', 'No such file or directory', 2)
我不是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)
现在运行时不会出现错误,但是当我尝试从公共驱动器读取文件时,会出现以下错误:

   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.
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读取数据,是否需要笔记本用户进行双重身份验证?


感谢您对文档或示例的任何提示

没有办法做到这一点。OAuth的作用域不同,一个是针对Google驱动器文件系统;另一个是谷歌云SDK