首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Google Colab中使用Python生成Google API access_token

在Google Colab中使用Python生成Google API access_token
EN

Stack Overflow用户
提问于 2020-02-18 01:41:29
回答 1查看 1.5K关注 0票数 5

我需要从Google Colab中的Python中生成一个具有特定作用域['https://www.googleapis.com/auth/bigquery', 'https://www.googleapis.com/auth/cloud-platform']access_token

默认情况下,Google Colab在colabtools中提供了一个函数来验证用户身份。

代码语言:javascript
复制
from google.colab import auth
auth.authenticate_user()
print('Authenticated')

但是生成的凭证文件包含固定作用域的列表,不包括我需要的作用域。

代码语言:javascript
复制
import os
with open(os.environ['GOOGLE_APPLICATION_CREDENTIALS']) as f:
  print(f.read())
代码语言:javascript
复制
...
"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-authenter code here使用google.auth.Credentials类和google.oauth2.Credentials类生成一个有效的凭证令牌。我可以生成有效的类实例,但是当我检查令牌时,它是None。

有没有办法通过Python中的自定义范围为Google API生成有效的access_token

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2020-02-18 02:56:26

如果您正在使用具有Google Colab的VM,则可以使用以下命令(示例)在该vm上设置服务帐户的作用域:

代码语言:javascript
复制
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中进行身份验证时,生成的凭据将包含正确的作用域。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60267827

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档