我需要从Google Colab中的Python中生成一个具有特定作用域['https://www.googleapis.com/auth/bigquery', 'https://www.googleapis.com/auth/cloud-platform']的access_token。
默认情况下,Google Colab在colabtools中提供了一个函数来验证用户身份。
from google.colab import auth
auth.authenticate_user()
print('Authenticated')但是生成的凭证文件包含固定作用域的列表,不包括我需要的作用域。
import os
with open(os.environ['GOOGLE_APPLICATION_CREDENTIALS']) as f:
print(f.read())...
"scopes": [
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/appengine.admin",
"https://www.googleapis.com/auth/accounts.reauth",
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/compute",
"https://www.googleapis.com/auth/userinfo.email"
],
...我已经尝试通过google-auth库enter code here使用google.auth.Credentials类和google.oauth2.Credentials类生成一个有效的凭证令牌。我可以生成有效的类实例,但是当我检查令牌时,它是None。
有没有办法通过Python中的自定义范围为Google API生成有效的access_token?
谢谢!
发布于 2020-02-18 02:56:26
如果您正在使用具有Google Colab的VM,则可以使用以下命令(示例)在该vm上设置服务帐户的作用域:
gcloud compute instances set-service-account example-instance \
--service-account my-sa-123@my-project-123.iam.gserviceaccount.com \
--scopes compute-rw,storage-ro然后,当从Python中进行身份验证时,生成的凭据将包含正确的作用域。
https://stackoverflow.com/questions/60267827
复制相似问题