简体   繁体   English

尝试使用Python查询Google BigQuery时出现“需要登录”错误

[英]Error “Login Required” when trying to query Google BigQuery with Python

I'd like to access BigQuery data from my local Linux machine with Python. 我想用Python从本地Linux机器上访问BigQuery数据。 The code from Google help https://cloud.google.com/bigquery/authorization#service-accounts-server works fine giving me the list of datasets. 来自Google帮助https://cloud.google.com/bigquery/authorization#service-accounts-server的代码可以很好地为我提供数据集列表。 But the query send via service library 但是查询通过服务库发送

SELECT id, name FROM [test_articles.countries] LIMIT 100

fail with "Login Required" message: 失败并显示“需要登录”消息:

googleapiclient.errors.HttpError: <HttpError 401 when requesting https://www.googleapis.com/bigquery/v2/projects/myproject/queries?alt=json returned "Login Required">

In BigQuery Web UI the query works fine. 在BigQuery Web UI中,查询工作正常。 For auth I use " Google APIs service account" from my Google Development console with permission "Can edit". 对于身份验证,我使用Google开发控制台中的“Google API服务帐户”,并允许“可以编辑”。

Here is the whole code 这是整个代码

import httplib2

from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials

# REPLACE WITH YOUR Project ID
PROJECT_ID = "xxxxxx"
# REPLACE WITH THE SERVICE ACCOUNT EMAIL FROM GOOGLE DEV CONSOLE
SERVICE_ACCOUNT_EMAIL = 'xxxxxxx@developer.gserviceaccount.com'

f = file('xxxxx.p12', 'rb')
key = f.read()
f.close()

credentials = SignedJwtAssertionCredentials(
    SERVICE_ACCOUNT_EMAIL,
    key,
    scope='https://www.googleapis.com/auth/bigquery')

http = httplib2.Http()
http = credentials.authorize(http)

service = build('bigquery', 'v2')
datasets = service.datasets()
response = datasets.list(projectId=PROJECT_ID).execute(http)

print('Dataset list:\n')
for dataset in response['datasets']:
  print("%s\n" % dataset['id'])


# SELECT data

query_data = {'query':'
    SELECT id, name FROM [test_articles.countries] LIMIT 100;
'}
query_request = service.jobs()
query_response = query_request.query(projectId=PROJECT_ID, 
    body=query_data).execute()
pp.pprint(query_response)

Instead of: 代替:

service = build('bigquery', 'v2')
datasets = service.datasets()
response = datasets.list(projectId=PROJECT_ID).execute(http)

Try: 尝试:

service = build('bigquery', 'v2', http=http)
datasets = service.datasets()
response = datasets.list(projectId=PROJECT_ID).execute()

(use the authenticated http connection when building the service) (在构建服务时使用经过身份验证的http连接)

确保通过终端登录您的gcloud帐户:

gcloud auth application-default login

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

相关问题 尝试使用python客户端访问BigQuery时出现身份验证错误 - Authentication error when trying to access BigQuery using python client 导入google-cloud-bigquery python模块时出错 - error when importing google-cloud-bigquery python module 尝试连接到Google Finance Python时出错 - Error when trying to connect to google finance Python Google BigQuery - 未正确解析Python查询 - Google BigQuery - Python Query not being parsed correctly 尝试将Google App Engine中的insertAll用于BigQuery时出现HTTP 501错误 - HTTP 501 error when trying to use insertAll from Google App Engine to BigQuery Python 中的 Google BigQuery 查询在使用 result() 时有效,但在使用 to_dataframe() 时出现权限问题 - Google BigQuery query in Python works when using result(), but Permission issue when using to_dataframe() Python - Quickfix:尝试登录时出现getHeader()属性错误 - Python - Quickfix : getHeader() attribute error when trying to login Google BigQuery Python-错误分页表 - Google BigQuery python - error paginating table 将Google adwords API与Python结合使用时,出现authenticationError.login_cookie_required错误 - Getting authenticationerror.login_cookie_required error while using Google adwords API with Python 尝试使用来自 google.cloud 的 BigQuery 时出现 ModuleNotFoundError - ModuleNotFoundError when trying to use BigQuery from google.cloud
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM