1

我使用 Colaboratory Notebook 并尝试在使用 PyDrive 库时自动化 GoogleAuth 过程。

我尝试了 dano 在这里提出的方式:https ://stackoverflow.com/a/24542604/10131744

不过,我收到一个错误,与客户端密码相关联。

这是我的代码:

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

auth.authenticate_user()
gauth = GoogleAuth()
#gauth.credentials = GoogleCredentials.get_application_default()
gauth.LoadCredentialsFile("mycreds.txt")
if gauth.credentials is None:
  # Authenticate if they're not there
  gauth.LocalWebserverAuth()
elif gauth.access_token_expired:
  # Refresh them if expired
  gauth.Refresh()
else:
  # Initialize the saved creds
  gauth.Authorize()
gauth.SaveCredentialsFile("mycreds.txt")

drive = GoogleDrive(gauth)

这是我得到的信息:

/usr/local/lib/python3.6/dist-packages/oauth2client/_helpers.py:255: UserWarning: Cannot access mycreds.txt: No such file or directory
  warnings.warn(_MISSING_FILE_MESSAGE.format(filename))
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/oauth2client/clientsecrets.py in _loadfile(filename)
    120     try:
--> 121         with open(filename, 'r') as fp:
    122             obj = json.load(fp)

FileNotFoundError: [Errno 2] No such file or directory: 'client_secrets.json'

During handling of the above exception, another exception occurred:

InvalidClientSecretsError                 Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/pydrive/auth.py in LoadClientConfigFile(self, client_config_file)
    385     try:
--> 386       client_type, client_info = clientsecrets.loadfile(client_config_file)
    387     except clientsecrets.InvalidClientSecretsError as error:

/usr/local/lib/python3.6/dist-packages/oauth2client/clientsecrets.py in loadfile(filename, cache)
    164     if not cache:
--> 165         return _loadfile(filename)
    166 

/usr/local/lib/python3.6/dist-packages/oauth2client/clientsecrets.py in _loadfile(filename)
    124         raise InvalidClientSecretsError('Error opening file', exc.filename,
--> 125                                         exc.strerror, exc.errno)
    126     return _validate_clientsecrets(obj)

InvalidClientSecretsError: ('Error opening file', 'client_secrets.json', 'No such file or directory', 2)

During handling of the above exception, another exception occurred:

InvalidConfigError                        Traceback (most recent call last)
<ipython-input-9-370983bd3c5e> in <module>()
     13 if gauth.credentials is None:
     14   # Authenticate if they're not there
---> 15   gauth.LocalWebserverAuth()
     16 elif gauth.access_token_expired:
     17   # Refresh them if expired

/usr/local/lib/python3.6/dist-packages/pydrive/auth.py in _decorated(self, *args, **kwargs)
    111       self.LoadCredentials()
    112     if self.flow is None:
--> 113       self.GetFlow()
    114     if self.credentials is None:
    115       code = decoratee(self, *args, **kwargs)

/usr/local/lib/python3.6/dist-packages/pydrive/auth.py in GetFlow(self)
    441     if not all(config in self.client_config \
    442                for config in self.CLIENT_CONFIGS_LIST):
--> 443       self.LoadClientConfig()
    444     constructor_kwargs = {
    445         'redirect_uri': self.client_config['redirect_uri'],

/usr/local/lib/python3.6/dist-packages/pydrive/auth.py in LoadClientConfig(self, backend)
    364         raise InvalidConfigError('Please specify client config backend')
    365     if backend == 'file':
--> 366       self.LoadClientConfigFile()
    367     elif backend == 'settings':
    368       self.LoadClientConfigSettings()

/usr/local/lib/python3.6/dist-packages/pydrive/auth.py in LoadClientConfigFile(self, client_config_file)
    386       client_type, client_info = clientsecrets.loadfile(client_config_file)
    387     except clientsecrets.InvalidClientSecretsError as error:
--> 388       raise InvalidConfigError('Invalid client secrets file %s' % error)
    389     if not client_type in (clientsecrets.TYPE_WEB,
    390                            clientsecrets.TYPE_INSTALLED):

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

我试图根据这个答案添加一个 client_secrets.json 文件:https ://stackoverflow.com/a/33426759/10131744

但是要么我做错了,要么 .json 文件不在正确的位置,但它不起作用。

非常感谢你的帮助。

4

0 回答 0