使用Google Colaboratory和Google Cloud时,必须对自己进行身份验证:
from google.colab import auth
auth.authenticate_user()
print('Authenticated')
运行该单元格的结果是:
Go to the following link in your browser:
https://accounts.google.com/o/oauth2/......
Enter verification code:
目前我必须授予它访问权限并在每次开始新笔记本时输入新密码,这让我觉得我做错了。
有没有办法永久允许Google Cloud SDK访问我的Google帐户?
您可以以在磁盘上写入文件的API密钥的形式保存凭据。这仍然需要在每个笔记本中运行一个单元进行身份验证,但至少您不必每次都单击身份验证流。
请参阅Getting Started with Authentication以获取服务帐户的API密钥。
然后,在您的笔记本中,写下如下内容:
import os
storage_auth_info = r"""YOUR API KEY HERE"""
with open('/tmp/storage_auth_info.json', 'w') as f:
f.write(storage_auth_info)
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = '/tmp/storage_auth_info.json'
这将允许有权访问您的笔记本的任何人使用保存的凭据,因此请谨慎使用,例如,考虑仅向服务帐户授予对您数据的只读访问权限。