我已按照 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">
如果我从本地主机测试,答案是:
HttpError: <HttpError 401 when requesting https://www.googleapis.com/bigquery/v2/projects/XXXXXXXX/queries?alt=json returned "Invalid Credentials">
有人可以帮助我吗?:-)