python - HttpError : <HttpError 400 when requesting https://www. googleapis.com/bigquery/v2/projects/

标签 python google-app-engine google-bigquery google-api-python-client

这是我在尝试对 bigquery 进行身份验证调用时遇到的错误

HttpError: <HttpError 400 when requesting https://www.googleapis.com/bigquery/v2/projects/ClientId/datasets/samples/tables/natality?alt=json returned "Invalid project ID 'ClientId'. Project IDs must contain 6-63 lowercase letters, digits, or dashes. IDs must start with a letter and may not end with a dash.">

这是我的 main.py

import httplib2
import os
from google.appengine.api import memcache
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from oauth2client.appengine import oauth2decorator_from_clientsecrets
from bqclient import BigQueryClient


PROJECT_ID = "########"  this is the Client Id 
DATASET = "samples"
TABLE = "natality"


CLIENT_SECRETS = os.path.join(os.path.dirname(__file__),
    'client_secrets.json')

http = httplib2.Http(memcache)
decorator = oauth2decorator_from_clientsecrets(CLIENT_SECRETS,
    'https://www.googleapis.com/auth/bigquery')

bq = BigQueryClient(http, decorator)

class MainHandler(webapp.RequestHandler):
    @decorator.oauth_required
    def get(self):
        self.response.out.write("Hello Dashboard!\n")
        modTime = bq.getLastModTime(PROJECT_ID, DATASET, TABLE)
        if modTime is not None:
            msg = 'Last mod time = ' + modTime
        else:
            msg = "Could not find last modification time.\n"
        self.response.out.write(msg)

application = webapp.WSGIApplication([
   ('/', MainHandler),
   (decorator.callback_path, decorator.callback_handler())
], debug=True)

def main():
   run_wsgi_app(application)

if __name__ == '__main__':
    main()

这是 app.yaml

application: hellomydashboard
version: 1
runtime: python
api_version: 1

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

- url: .*
  script: main.py

这是 bqclient.py

import httplib2
from apiclient.discovery import build
from oauth2client.appengine import oauth2decorator_from_clientsecrets

class BigQueryClient(object):
    def __init__(self, http, decorator):
        """Creates the BigQuery client connection"""
        self.service = build('bigquery', 'v2', http=http)
        self.decorator = decorator

    def getTableData(self, project, dataset, table):
        decorated = self.decorator.http()
        return self.service.tables().get(projectId=project, datasetId=dataset,
            tableId=table).execute(decorated)

    def getLastModTime(self, project, dataset, table):
        data = self.getTableData(project, dataset, table)
        if data is not None and 'lastModifiedTime' in data:
            return data['lastModifiedTime']
        else:
            return None

    def Query(self, query, project, timeout_ms=10000):
        query_config = {
            'query': query,
            'timeoutMs': timeout_ms
        }
        decorated = self.decorator.http()
        result_json = (self.service.jobs()
                       .query(projectId=project, body=query_config)
                       .execute(decorated))

        return result_json

我还尝试将 ClientId 替换为项目 Id,如错误中所述,但它给出了另一个错误

HttpError: <HttpError 404 when requesting https://www.googleapis.com/bigquery/v2/projects/hellodashboard87/datasets/samples/tables/natality?alt=json returned "Not Found: Dataset hellodashboard87:samples">

我正在按照本页上的教程进行操作 https://developers.google.com/bigquery/articles/dashboard#firstcall

最佳答案

为了使用 Google BigQuery 提供的公共(public)数据集,请使用以下参数:

项目 ID:publicdata

数据集 ID:样本

表格 ID:出生率(或您想要使用的任何内容)

要使用您拥有的任何数据集,请将您的项目 ID 切换为 API 控制台仪表板中的项目 ID。

关于python - HttpError : <HttpError 400 when requesting https://www. googleapis.com/bigquery/v2/projects/,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13244750/

相关文章:

python - 如何在 Python 中异步接收来自多个 WebSocket 的数据?

python - 谷歌应用引擎 : Task queue performance

python - 通过 bigquery-python 库向 BigQuery 插入大量数据

google-bigquery - 在分区表上使用 FIRST 或 LAST 返回不一致的值

macos - 安装 Google Go 和 App Engine SDK 的正确方法是什么?

google-bigquery - 如何将 Bigquery 结果保存为 PHP 脚本中的表?

python - 在 Python 中匹配 Unicode 字边界

python - 下拉列表更改后来自模板的 Django AJAX 调用/url

Python 继承 : Return subclass

python - 升级到 GAE3 时替换 google.appengine.api 导入模块