【问题标题】:Task queue authentication from Compute Engine - "Insufficient Permission"来自 Compute Engine 的任务队列身份验证 - “权限不足”
【发布时间】:2016-08-05 15:07:54
【问题描述】:

我正在尝试使用 Google 的 python REST API 库从计算引擎 VM 实例中使用拉取队列,并不断收到 403 错误 - “权限不足”

queue.yaml:

queue:
- name: queuename
  mode: pull

构建 api 客户端:

from apiclient.discovery import build
credentials = GoogleCredentials.get_application_default()
task_api = build('taskqueue', 'v1beta2', credentials=credentials)

获取任务:

lease_req = task_api.tasks().lease(project=PROJECT_NAME,
                                           taskqueue=QUEUE_NAME,
                                           leaseSecs=60 * 30,
                                           numTasks=1)
result = lease_req.execute()

结果总是 HttpError 403 - Insufficient Permission

VM 已启用完整的云平台 API 访问。

【问题讨论】:

    标签: python google-app-engine google-compute-engine


    【解决方案1】:

    您应该可以通过GoogleCredentials.get_application_default 从 GCE 访问任务队列:

    1. 您使用的服务器必须启用任务队列、云 API 访问范围。见this discussion
    2. 或者服务器必须以正确的权限连接到 Google 帐户,即正确的密钥必须在 /root/.config/gcloud/application_default_credentials.json 中(如果该进程在另一个用户下运行,则在用户主页中搜索密钥)。
    3. 或将环境变量GOOGLE_APPLICATION_CREDENTIALS 指向正确的json 密钥文件(您需要service_account 密钥,而不是authorized_user)。

    service_account 键必须有权访问正确的范围,https://www.googleapis.com/auth/taskqueuehttps://www.googleapis.com/auth/taskqueue.consumer。 (要生成 OAuth2 凭据,请转到网络 Google GAE 控制台API Manager - 凭据

    你必须在queue.yaml中设置队列ACL:

    queue:
    - name: myqueue
      mode: pull
      acl:
      - user_email: myemail@myemailserver.com
      - writer_email: myemail@myemailserver.com
    

    常规 REST API 访问

    您可以从任何机器访问 TaskQueue REST API。您也可以手动加载凭据:

    from oauth2client.service_account import ServiceAccountCredentials
    # use the right scopes by the queue ACL
    SCOPES = ['https://www.googleapis.com/auth/taskqueue']
    credentials = ServiceAccountCredentials.from_json_keyfile_name('myemail.json', scopes=SCOPES)
    # or
    credentials = ServiceAccountCredentials.from_p12_keyfile('myemail@myemailserver.com', '/path/to/myemail.p12', scopes=SCOPES)
    

    【讨论】:

    • 老问题.. 我已经使用带有 .pem 密钥文件的 SignedJwtAssertionCredentials 和带有 user_email、writer_email 帐户的队列 ACL 修复了它。没有尝试您的解决方案,因为它与我不再相关,但它看起来正确,设置为接受的答案。
    【解决方案2】:

    问题在于 TaskQueue API 只是 App Engine 的一项功能,因此无法从 Google Compute Engine 访问。

    【讨论】:

    • TaskQueue 可以通过 REST API 在 GAE 之外访问,这就是问题中使用的内容。
    猜你喜欢
    • 2018-08-21
    • 1970-01-01
    • 2019-07-06
    • 1970-01-01
    • 1970-01-01
    • 2021-08-11
    • 1970-01-01
    • 1970-01-01
    • 2011-03-05
    相关资源
    最近更新 更多