【问题标题】:HttpError: <HttpError 400 when requesting https://www.googleapis.com/bigquery/v2/projects/HttpError:<HttpError 400 请求 https://www.googleapis.com/bigquery/v2/projects/ 时
【发布时间】:2012-10-26 00:50:33
【问题描述】:

这是我在尝试对 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

我也尝试用错误中所述的项目 ID 替换 ClientId,但它给出了另一个错误

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

【问题讨论】:

  • 您发布的错误消息说明了一切:“无效的项目 ID 'ClientId'。项目 ID 必须包含 6-63 个小写字母、数字或短划线。” .为什么不尝试将PROJECT_ID 更改为project-id,即仅使用小写 字母、数字或破折号。
  • 是的,我确实用项目 ID 尝试过,但它给出了另一个错误 HttpError: googleapis.com/bigquery/v2/projects/hellodashboard87/datasets/… returned "Not Found: Dataset hellodashboard87:samples">
  • 您可以尝试使用developers.google.com/apis-explorer/#p/bigquery/v2/… 来测试您的输入是否准确。您可以通过此方法更轻松地使用 API 来快速测试您的输入值。
  • 好吧,我按照你说的尝试过,但它显示-----错误 404 Not Found - 显示标题 - { "error": { "errors": [ { "domain": "global" ,“原因”:“未找到”,“消息”:“未找到:数据集 440265227336:样本”}],“代码”:404,“消息”:“未找到:数据集 440265227336:样本”} }
  • @someone1 它还显示 Request GET googleapis.com/bigquery/v2/projects/440265227336/datasets/…{YOUR_API_KEY} Authorization: Bearer ya29.AHES6ZQDddqS_qBwjpn3ngTaaYf33oUScSW2vWtTCYR4z9sGErxX X-JavaScript-User-Agent: Google APIs Explorer 我需要一个 API 密钥吗。如果需要怎么办这样做。没有字段可以输入 api 密钥

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


【解决方案1】:

要使用 Google 的 BigQuery 提供的公共数据集,请使用以下参数:

项目 IDpublicdata

数据集 IDsamples

表 IDnatality(或任何你想使用的)

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

【讨论】:

  • 所以为了使用公共数据,我不需要在 google api 控制台上创建自己的项目或任何东西。但在这种情况下,我将如何进行身份验证
  • 您当前的身份验证方法应该可以工作,您是否收到任何错误?
【解决方案2】:

为了使用 BigQuery,您必须在 APIs Console 中创建一个启用 BigQuery 的项目(我假设您已经这样做了)。创建项目后,您将能够从 URL 中获取 项目编号,例如

https://code.google.com/apis/console/#project:12345XXXXXXX

在示例中,项目编号为 12345XXXXXXX,这是您将用于 PROJECT_ID 的值。

【讨论】:

  • 好吧,我做了完全相同的事情,但仍然收到这个错误,说 Invalid Project ID
  • 您已经在 API 控制台中正确创建了项目?您对 PROJECT_ID 使用什么值?
  • 我错了,请看我编辑的答案。您需要来自项目的项目编号,而不是客户 ID(尽管项目编号经常出现在客户 ID 中)。
  • 仍然收到另一个错误 HttpError: googleapis.com/bigquery/v2/projects/440265227336/datasets/… returned "Not Found: Dataset 440265227336:samples">
  • 您现在遇到了另一类问题,它肯定是正确的身份验证。您需要调整 DATASETTABLE 以匹配您项目中的内容。
猜你喜欢
  • 1970-01-01
  • 2013-04-25
  • 2013-07-28
  • 2018-12-06
  • 1970-01-01
  • 2017-03-23
  • 2014-09-05
  • 1970-01-01
  • 2022-07-28
相关资源
最近更新 更多