HttpError 403 when requesting https://www.googleapis.com/bigquery/v2/projects/publicdata/queries?alt=json returned "Access Denied: Job publicdata:
Question: i'm getting the following error while trying to run a query on Bigquery using
GAE python.

    
    
    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">
    

Here is main.py code

    
    
    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()
    

Here is 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
    

i'm using App Engine Service Account for authentication


Answer: It looks like you are trying to run a query under the 'publicdata' project
(which is internal to BigQuery, and thus you don't have permission to access
it). Queries must be run using the Project Number of the Google Developer
project you created. See the [BigQuery REST API Quick
Start](https://developers.google.com/bigquery/docs/hello_bigquery_api) page
for more information.

One thing: your Class name in the example is "GetTableData" - not sure if you
are trying to [List
Tabledata](https://developers.google.com/bigquery/docs/reference/v2/tabledata/list),
or retrieve a [Table
resource](https://developers.google.com/bigquery/docs/reference/v2/tables/get)?
In any case, here are some Python snippets that demonstrate how you might make
these API calls using the [Google Python API
client](http://code.google.com/p/google-api-python-client/).

    
    
        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
    

