在 Google Colab 中使用 Drive Mount 时出错

Nac*_*gue 18 python google-drive-api google-colaboratory

我一直在使用 Colab 工作:

from google.colab import drive 
.mount('/content/gdrive')

Run Code Online (Sandbox Code Playgroud)

没有问题,直到今天。

我不知道为什么会出现此错误:

Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=947318989803-6bn6qk8qdgf4n4g3pfee6491hc0brc4i.apps.googleusercontent.com&redirect_uri=urn%3aietf%3awg%3aoauth%3a2.0%3aoob&scope=email%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdocs.test%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive.photos.readonly%20https%3a%2f%2fwww.googleapis.com%2fauth%2fpeopleapi.readonly&response_type=code

Enter your authorization code:
··········
---------------------------------------------------------------------------
TIMEOUT                                   Traceback (most recent call last)
<ipython-input-4-d05fe204dd76> in <module>()
      1 from google.colab import drive
----> 2 drive.mount('/content/gdrive')

4 frames
/usr/local/lib/python3.6/dist-packages/google/colab/drive.py in mount(mountpoint, force_remount, timeout_ms, use_metadata_server)
    223         oauth_prompt,
    224         problem_and_stopped,
--> 225         drive_exited,
    226     ])
    227     if case == 0:

/usr/local/lib/python3.6/dist-packages/pexpect/spawnbase.py in expect(self, pattern, timeout, searchwindowsize, async_, **kw)
    342         compiled_pattern_list = self.compile_pattern_list(pattern)
    343         return self.expect_list(compiled_pattern_list,
--> 344                 timeout, searchwindowsize, async_)
    345 
    346     def expect_list(self, pattern_list, timeout=-1, searchwindowsize=-1,

/usr/local/lib/python3.6/dist-packages/pexpect/spawnbase.py in expect_list(self, pattern_list, timeout, searchwindowsize, async_, **kw)
    370             return expect_async(exp, timeout)
    371         else:
--> 372             return exp.expect_loop(timeout)
    373 
    374     def expect_exact(self, pattern_list, timeout=-1, searchwindowsize=-1,

/usr/local/lib/python3.6/dist-packages/pexpect/expect.py in expect_loop(self, timeout)
    179             return self.eof(e)
    180         except TIMEOUT as e:
--> 181             return self.timeout(e)
    182         except:
    183             self.errored()

/usr/local/lib/python3.6/dist-packages/pexpect/expect.py in timeout(self, err)
    142             exc = TIMEOUT(msg)
    143             exc.__cause__ = None    # in Python 3.x we can use "raise exc from None"
--> 144             raise exc
    145 
    146     def errored(self):

TIMEOUT: <pexpect.popen_spawn.PopenSpawn object at 0x7f58fb7ab4e0>
searcher: searcher_re:
    0: re.compile('google.colab.drive MOUNTED')
    1: re.compile('root@e7a376888b95-b97a84955c154714b7850ceb4ecf0e8e: ')
    2: re.compile('(Go to this URL in a browser: https://.*)$')
    3: re.compile('Drive File Stream encountered a problem and has stopped')
    4: re.compile('drive EXITED')
<pexpect.popen_spawn.PopenSpawn object at 0x7f58fb7ab4e0>
searcher: searcher_re:
    0: re.compile('google.colab.drive MOUNTED')
    1: re.compile('root@e7a376888b95-b97a84955c154714b7850ceb4ecf0e8e: ')
    2: re.compile('(Go to this URL in a browser: https://.*)$')
    3: re.compile('Drive File Stream encountered a problem and has stopped')
    4: re.compile('drive EXITED')
Run Code Online (Sandbox Code Playgroud)

众所周知,这段代码会打开一个新标签页,您必须在其中选择一个 Google 帐户,单击“允许”按钮,然后将一个长密码复制到 Colab

编辑:

我试图以另一种方式做同样的事情:

#Installing PyDrive
!pip install PyDrive

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

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

#Getting the file
downloaded2 = drive.CreateFile({'id':"1QGtoo1wqCP2yrjl8kFu4kTfjWqh3EOdt"})   # replace the id with id of file you want to access
downloaded2.GetContentFile('estructura_cc_felipe.xlsx') 
Run Code Online (Sandbox Code Playgroud)

但它引发了这个错误:

Go to the following link in your browser:

    https://accounts.google.com/o/oauth2/auth?client_id=32555940559.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=openid+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fappengine.admin+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcompute+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Faccounts.reauth+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&code_challenge=WkM1RS8Flu1_txc3jn4V_FfutgZuRaHSzYbDvs134PM&code_challenge_method=S256&access_type=offline&response_type=code&prompt=select_account


Enter verification code: ··········
---------------------------------------------------------------------------
AuthorizationError                        Traceback (most recent call last)
<ipython-input-2-bb96e063f8ef> in <module>()
      6 
      7 #Authenticating and creating the PyDrive client
----> 8 auth.authenticate_user()
      9 gauth = GoogleAuth()
     10 gauth.credentials = GoogleCredentials.get_application_default()

1 frames
/usr/local/lib/python3.6/dist-packages/google/colab/auth.py in authenticate_user(clear_output)
    147     context_manager = _output.temporary if clear_output else _noop
    148     with context_manager():
--> 149       _gcloud_login()
    150     _install_adc()
    151     colab_tpu_addr = _os.environ.get('COLAB_TPU_ADDR', '')

/usr/local/lib/python3.6/dist-packages/google/colab/auth.py in _gcloud_login()
     97     _os.remove(name)
     98   if gcloud_process.returncode:
---> 99     raise _errors.AuthorizationError('Error fetching credentials')
    100 
    101 

AuthorizationError: Error fetching credentials
Run Code Online (Sandbox Code Playgroud)

有谁知道出了什么问题?我长期以来一直在导入位于 Google 驱动器上的文件,但从未遇到过这些问题。

kan*_*ndi 36

事实证明,如果您从 auth 按钮手动复制 auth 代码而不是单击复制按钮,它会起作用