如何在没有最终用户交互的情况下以编程方式从Python查询BigQuery?

时间:2012-11-03 20:04:16

标签: python google-bigquery

这个问题似乎应该如此简单地回答,但经过几天的研究和几个死胡同之后,如果没有坚持基于用户的OAuth,我似乎无法从BigQuery获得查询结果。有人有运气吗?我没有将Google AppEngine用于我的应用,它托管在EC2中。具体情况如下:

User wants reporting data -->
Web server makes queries to BigQuery -->
Data is transformed for use in WebApp and returned to User.

每当我关注Google示例时,我最终都会弹出一个网络浏览器,要求我选择一个用于身份验证的Google帐户。

3 个答案:

答案 0 :(得分:16)

很抱歉,查找信息非常具有挑战性。您正在寻找Service Accounts指南中记录的Authorizing Access to the BigQuery API using OAuth 2.0所谓的内容。

以下是使用Python client library的示例,但您需要查看引用文档以获取有关获取相应凭据的信息:

import httplib2

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

# REPLACE WITH YOUR Project ID
PROJECT_NUMBER = 'XXXXXXXXXXX'
# REPLACE WITH THE SERVICE ACCOUNT EMAIL FROM GOOGLE DEV CONSOLE
SERVICE_ACCOUNT_EMAIL = 'XXXXX@developer.gserviceaccount.com'

# OBTAIN THE KEY FROM THE GOOGLE APIs CONSOLE
# More instructions here: http://goo.gl/w0YA0
f = file('key.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_NUMBER).execute(http)

print 'Dataset list:'
for dataset in response['datasets']:
  print '%s' % dataset['datasetReference']['datasetId']

答案 1 :(得分:4)

如果您使用gcloud在本地登录:

gcloud auth application-default login

然后一个凭证文件将存储在〜/ .config / gcloud /中,可以加载:

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

credentials = GoogleCredentials.get_application_default()
service = build('bigquery', 'v2')

或者您可以直接加载BQ安全密钥文件:

from google.cloud import bigquery
client = bigquery.Client.from_service_account_json(path_to_key.json)

答案 2 :(得分:0)

我有同样的问题。这可能是因为您在项目中没有所使用的服务帐户密钥的必要权限。