简体   繁体   English

BigQuery API REST 使用 Python 的身份验证问题

[英]Authentification issue to BigQuery API REST using Python

I would like to make a HTTP call to this resource: https://bigquery.googleapis.com/bigquery/v2/projects/{projectId}/jobs我想对该资源进行 HTTP 调用: https://bigquery.googleapis.com/bigquery/v2/projects/{projectId}/jobs

As I read to the documentation I use an API key generated from my GCP project to be authenticated.当我阅读文档时,我使用从我的 GCP 项目生成的 API 密钥进行身份验证。 So with requests I make a simple call like this:因此,对于requests ,我进行了这样的简单调用:

import requests

params = {'key': 'MY_API_KEY'}
base_url = 'https://bigquery.googleapis.com'
project_id = 'MY_PROJECT_ID'
r = requests.get(f'{base_url}/bigquery/v2/projects/{project_id}/jobs', params=params)

Unfortunately it returns a response 401 and I can't figure out why.不幸的是,它返回响应 401,我不知道为什么。

Thanks a lot and have a nice day !非常感谢,祝您有美好的一天!

Update code after guillaume blaquiere reply: guillaume blaquiere 回复后更新代码:

from google.auth.transport.requests import AuthorizedSession
from google.oauth2 import service_account

base_url = 'https://bigquery.googleapis.com'
project_id = 'project_id'

credentials = service_account.Credentials.from_service_account_file(
    'service_account.json',
    scopes=['https://www.googleapis.com/auth/bigquery',
            'https://www.googleapis.com/auth/cloud-platform'],
)

authed_session = AuthorizedSession(credentials)
response = authed_session.request('GET', f'{base_url}/bigquery/v2/projects/{project_id}/jobs')
print(response.json())

# this returns : {'etag': 'tAZvk1k2f2GY8yHaQF7how==', 'kind': 'bigquery#jobList'}

The API Key no longer works for a large number of Google API. API 密钥不再适用于大量 Google API。 Only some legacy continue to accept an API key.只有一些遗留系统继续接受 API 密钥。

Now, you need an authenticated request.现在,您需要一个经过身份验证的请求。 You can find exemple in the google-auth python library documentation .您可以在google-auth python 库文档中找到示例。 Look at Refresh and Authorized_session.查看 Refresh 和 Authorized_session。

Don't hesitate to comment if you need help about the credential obtention, I can also help you on this.如果您需要有关凭证获取的帮助,请随时发表评论,我也可以为您提供帮助。

EDIT编辑

When you perform the request, it's, by default, only on the current user.当您执行请求时,默认情况下,它只针对当前用户。 In your case, it's the service account when you use the Python code, and your User account when you use the API Explorer (the swagger like in the Google Documentation).在您的情况下,当您使用 Python 代码时它是服务帐户,当您使用 API 资源管理器时它是您的用户帐户(swagger 就像在 Google 文档中一样)。

In your case, I guess that your service account has never performed a job (query or load job) and thus, there is no entry for it.在您的情况下,我猜您的服务帐户从未执行过作业(查询或加载作业),因此没有条目。

According with the documentation , is you want to see all the user jobs, you have to add the param ?allUsers=true at the end of your URL根据文档,您是否想查看所有用户作业,您必须在 URL 末尾添加参数?allUsers=true

response = authed_session.request('GET', f'{base_url}/bigquery/v2/projects/{project_id}/jobs?allUsers=true')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM