如何在没有最终用户交互的情况下从Python以编程方式查询BigQuery? [英] How to query BigQuery programmatically from Python without end-user interaction?

查看:350
本文介绍了如何在没有最终用户交互的情况下从Python以编程方式查询BigQuery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题似乎应该很容易回答,但经过几天的研究和几个死路一条,我似乎无法从 BigQuery 中查询结果。没有它坚持基于用户的OAuth。有没有人有这样的运气?我没有为我的应用程序使用 Google AppEngine ,它位于 EC2 中。确切情况如下:

This question seems like it should be so simple to answer, but after days of research and several dead ends, I can't seem to get query results out of BigQuery without it insisting on user-based OAuth. Has anyone had any luck with this? I am not using Google AppEngine for my app, it is hosted in EC2. Here is the exact situation:

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

无论何时我遵循Google示例,我最终都会弹出一个Web浏览器,要求我选择一个用于身份验证的Google帐户。

Whenever I follow the Google examples, I end up getting a web browser popping up asking for me to select a Google account to use for authentication.

推荐答案

对不起,这对于查找信息非常具有挑战性。您正在寻找所谓的服务帐户,这些帐户记录在我们的使用OAuth 2.0授权访问BigQuery API 指南。

Sorry this is being so challenging to find info on. You're looking for what's called Service Accounts which are documented in our Authorizing Access to the BigQuery API using OAuth 2.0 guide.

这是一个例如,使用Python 客户端库,但您需要查看引用的文档以获取有关获取适当的证书:

Here's an example, using the Python client library, though you'll want to look at the referenced documentation for info on acquiring the appropriate credentials:

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']

这篇关于如何在没有最终用户交互的情况下从Python以编程方式查询BigQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆