【问题标题】:Having trouble with authentication when doing rest api to retrieve Bigquery job information使用 rest api 检索 Bigquery 作业信息时遇到身份验证问题
【发布时间】:2017-05-21 22:18:23
【问题描述】:

我正在尝试执行 http 请求以获取由另一个脚本提交的作业的信息。该脚本具有作业 ID 和项目。我在 Oauth20 上阅读并看到使用 Application Default Credentials 是推荐的方式。 我已导出默认身份验证:

export GOOGLE_APPLICATION_CREDENTIALS= /test/proj-service-key-file.json

这是我的代码:

from google.oauth2.service_account import Credentials
from google.cloud import bigquery
import requests
from oauth2client.client import GoogleCredentials
from google.auth.transport.requests import AuthorizedSession 
import google.auth

credentials = GoogleCredentials.get_application_default()
session = google.auth.transport.requests.AuthorizedSession(credentials)
job_url="https://www.googleapis.com/bigquery/v2/projects/" + self.project + "/jobs/" + job_id + "?maxResults=1"
    job_query_results = session.request('GET',job_url)

我收到以下错误:
self.credentials.before_request( AttributeError: '_JWTAccessCredentials' 对象没有属性 'before_request'

感谢任何建议。

【问题讨论】:

    标签: google-bigquery google-authentication


    【解决方案1】:

    尝试删除导出语句中的空格:

    export GOOGLE_APPLICATION_CREDENTIALS=/test/proj-service-key-file.json

    【讨论】:

    • 感谢您的回复。当我在发布我的问题时尝试更改密钥文件的名称时,无意中添加了该空间。我在实际运行的导出 stmt 中并没有真正的空间。还有其他建议吗?
    【解决方案2】:

    我让它工作了。我必须明确地设置范围,然后就成功了:

        scopes = ['https://www.googleapis.com/auth/cloud-platform',
                        'https://www.googleapis.com/auth/devstorage.full_control',
                        'https://www.googleapis.com/auth/bigquery']
        credentials = credentials.create_scoped(scopes)
        http = httplib2.Http()
        if credentials.access_token_expired:
            credentials.refresh(http)
        http = credentials.authorize(http)
        response, content = http.request(job_url)
    

    【讨论】:

      猜你喜欢
      • 2017-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-16
      • 1970-01-01
      • 2012-08-29
      • 1970-01-01
      • 2018-02-28
      • 1970-01-01
      相关资源
      最近更新 更多