2

尝试使用 GAE python 在 Bigquery 上运行查询时出现以下错误。

HttpError 403 when requesting https://www.googleapis.com/bigquery/v2/projects/publicdata/queries?alt=json returned "Access Denied: Job publicdata:job_c08d8f254c0449c2b3e26202e62ca5fa: RUN_QUERY_JOB">

这是 main.py 代码

import httplib2
import os

from apiclient.discovery import build
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from oauth2client.appengine import AppAssertionCredentials

# BigQuery API Settings
SCOPE = 'https://www.googleapis.com/auth/bigquery'
PROJECT_NUMBER = 'publicdata' # REPLACE WITH YOUR Project ID

# Create a new API service for interacting with BigQuery
credentials = AppAssertionCredentials(scope=SCOPE)
httpss = credentials.authorize(httplib2.Http())
bigquery_service = build('bigquery', 'v2', http=httpss)




class GetTableData(webapp.RequestHandler):
  def get(self):
    queryData = {'query':'SELECT word,count(word) AS count FROM publicdata:samples.shakespeare GROUP BY word;',
             'timeoutMs':10000}

    queryData = bigquery_service.jobs()
    queryReply = queryData.query(projectId=PROJECT_NUMBER,body=queryData).execute()
    self.response.out.write(queryReply)



application = webapp.WSGIApplication(
                                     [('/queryTableData',GetTableData)
                                    ],
                                     debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()

这是 App.yaml

application: bigquerymashup
version: 1
runtime: python
api_version: 1

handlers:
- url: /favicon\.ico
  static_files: favicon.ico
  upload: favicon\.ico

- url: /css
  static_dir: css

- url: /js
  static_dir: js

- url: /img
  static_dir: img

- url: .*
  script: main.py

我正在使用 App Engine 服务帐户进行身份验证

4

1 回答 1

2

您似乎正在尝试在“publicdata”项目(BigQuery 内部,因此您无权访问它)下运行查询。必须使用您创建的 Google Developer 项目的项目编号来运行查询。如需了解详情,请参阅BigQuery REST API 快速入门页面。

一件事:您在示例中的类名称是“GetTableData” - 不确定您是尝试列出 Tabledata还是检索Table 资源?无论如何,这里有一些 Python 代码片段,它们演示了如何使用Google Python API 客户端进行这些 API 调用。

    def get_table(service, project_number, dataset_id, table_id):
      """Get Table information.

      Args:
        service: Authorized BigQuery API client.
        project_number: The current Project number.
        dataset_id: The name of the dataset.
        table_id: Id of the relevant table.
      """
      tables = service.tables()

      try:
        table_info = tables.get(projectId=project_number,
                                datasetId=dataset_id,
                                tableId=table_id).execute()
        print 'Table information:\n'
        print 'Table name: %s' % table_info['id']
        print 'Table creation time: %s' % table_info['creationTime']

      except errors.HttpError, error:
        print 'Could not get Table information: %s' % error


def list_table_data(service, project_number, dataset_id, table_id):
  """Returns table data from a specific set of rows.

  Args:
    service: Authorized BigQuery API client.
    project_number: The current Project number.
    dataset_id: The name of the dataset.
    table_id: The name of the table.
  """

  try:
    table = service.tabledata()
    table_data = table.list(projectId=project_number,
                       datasetId=dataset_id,
                       tableId=table_id,
                       maxResults=10).execute(http)

    print 'Total Rows: %s' % table_data['totalRows']
    for row in table_data['rows']:
      data = []
      for values in row['f']:
        value = values['v'] if values['v'] is not None else ''
        data.append(value)
      print '  '.join(data)

  except HttpError, error:
    print 'Could not list Table data. %s' % error
于 2012-11-19T06:42:00.370 回答