在执行 rest api 检索 Bigquery 作业信息时遇到身份验证问题

Having trouble with authentication when doing rest api to retrieve Bigquery job information

我正在尝试执行 http 请求以获取由另一个脚本提交的作业的信息。此脚本具有作业 ID 和项目。我阅读了 Oauth20 并发现使用应用程序默认凭证是推荐的方法。 我已导出默认身份验证:

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'

如有任何建议,我们将不胜感激。

尝试删除导出语句中的 space:

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

我让它工作了。我必须明确地设置范围并且成功了:

    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)