请求https://www.googleapis.com/bigquery/v2/projects/publicdata/queries?alt=json返回“HttpError 403”拒绝访问:作业publicdata:

时间:2012-11-18 16:23:28

标签: python google-app-engine google-bigquery

尝试使用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服务帐户进行身份验证

1 个答案:

答案 0 :(得分:2)

看起来您正在尝试在'publicdata'项目下运行查询(这是BigQuery内部的,因此您无权访问它)。必须使用您创建的Google Developer项目的项目编号运行查询。有关详细信息,请参阅BigQuery REST API Quick Start页面。

一件事:示例中的类名是“GetTableData” - 不确定您是尝试List Tabledata还是检索Table resource?在任何情况下,以下是一些Python代码段,演示了如何使用Google Python API client进行这些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