【发布时间】:2013-07-06 06:17:04
【问题描述】:
我已按照 https://developers.google.com/bigquery/authorization#service-accounts-appengine 进行一些从应用引擎到 bigquery 的查询。
在第 2 步中,我点击 Google Api 控制台中的团队,它会重定向到 App Engine > Administration > Permissions。我将服务帐户名称添加为电子邮件并选择开发人员作为角色(“可以编辑”选项不可用),然后单击“邀请用户”。之后,出现一条消息:“一封电子邮件已发送至 xxxxxx@appspot.gserviceaccount.com 进行验证。”状态为待处理。我如何确认电子邮件?,这里似乎有一个错误......
接下来,我用下面的代码做了一个测试:
#!/usr/bin/env python
import httplib2
import webapp2
from google.appengine.api import memcache
from apiclient.discovery import build
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from oauth2client.appengine import AppAssertionCredentials
# BigQuery API Settings
PROJECT_NUMBER = 'XXXXXXXX'
credentials = AppAssertionCredentials(scope='https://www.googleapis.com/auth/bigquery')
http = credentials.authorize(httplib2.Http(memcache))
service = build("bigquery", "v2", http=http)
class MainHandler(webapp2.RequestHandler):
def get(self):
query = {'query':'SELECT word,count(word) AS count FROM publicdata:samples.shakespeare GROUP BY word;',
'timeoutMs':10000}
jobRunner = service.jobs()
reply = jobRunner.query(projectId=PROJECT_NUMBER,body=query).execute()
self.response.out.write(reply)
app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True)
回复是(从谷歌运行):
HttpError: <HttpError 403 when requesting https://www.googleapis.com/bigquery/v2/projects/XXXXXXXX/queries?alt=json returned "Access Denied: Job YYYYYYYY:job_e57bdde0144c495dbc864ccbfb82b704: RUN_QUERY_JOB">
如果我从 localhost 测试,答案是:
HttpError: <HttpError 401 when requesting https://www.googleapis.com/bigquery/v2/projects/XXXXXXXX/queries?alt=json returned "Invalid Credentials">
有人可以帮助我吗? :-)
【问题讨论】:
-
这段代码对我来说完美无缺(当我用 BigQuery 项目 ID 替换 'XXX..' 时,在我的例子中是一个字符串),所以这是一个身份验证问题。顺便说一句,AppAssertionCredentials 在本地主机上不起作用。您是否在 code.google.com/apis/console/#project:ZZZ:services 上启用了 BigQuery API(以 ZZZ 作为您的 API 项目编号)?
-
如果在 App Engine 中,请检查 appengine.google.com/…。查看“Google API 控制台项目编号:”下的链接?点击它,并确保该项目启用了 BigQuery API。
标签: google-app-engine authorization google-bigquery server-to-server