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の内部にあるため、アクセスする権限がありません)でクエリを実行しようとしているようです。クエリは、作成したGoogleDeveloperプロジェクトのプロジェクト番号を使用して実行する必要があります。詳細については、BigQueryRESTAPIクイックスタートページをご覧ください。

1つ:例のクラス名は「GetTableData」です。Tabledataを一覧表示しようとしているのか、 Tableリソースを取得しようとしているのかわからない場合は、いずれにせよ、GooglePythonAPIクライアントを使用してこれらのAPI呼び出しを行う方法を示すいくつかのPythonスニペットがあります。

    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 に答える