BigQuery Python API问题

问题描述 投票:2回答:2

我今天花了一整天时间尝试为我的一个新项目设置python BigQuery API。我经历了整个过程

  1. 创建一个新项目
  2. 为项目启用计费
  3. 启用BigQuery API
  4. 创建服务帐户以连接到BigQuery API

我尝试了最简单的例子

import os
from google.cloud import bigquery

def main():
    # [START bigquery_quickstart]
    # Imports the Google Cloud client library

    # Instantiates a client
    creds = "absolute-path-to-file.json"
    os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = creds
    print(creds)
    bigquery_client = bigquery.Client().from_service_account_json(creds)

    # The name for the new dataset
    dataset_name = 'my_new_dataset'

    # Prepares the new dataset
    dataset = bigquery_client.dataset(dataset_name)

    # Creates the new dataset
    dataset.create()

    print('Dataset {} created.'.format(dataset.name))
# [END bigquery_quickstart]
if __name__ == '__main__':
    main()

并得到以下错误

Traceback (most recent call last):
  File "prepare-upload.py", line 121, in <module>
    main()
  File "prepare-upload.py", line 116, in main
    dataset.create()
  File "//anaconda/envs/tpot/lib/python3.5/site-packages/google/cloud/bigquery/dataset.py", line 431, in create
    method='POST', path=path, data=self._build_resource())
  File "//anaconda/envs/tpot/lib/python3.5/site-packages/google/cloud/_http.py", line 293, in api_request
    raise exceptions.from_http_response(response)
google.cloud.exceptions.Forbidden: 403 POST https://www.googleapis.com/bigquery/v2/projects/kobas-public-datasets-182301/datasets: Access Not Configured. BigQuery API has not been used in project 203593156286 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/bigquery.googleapis.com/overview?project=203593156286 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.

API已启用,我不确定是什么问题。让我感到困惑的是,我为其他两个项目完成了相同的过程,并且工作正常。再观察一次。错误建议去

https://console.developers.google.com/apis/api/bigquery.googleapis.com/overview?project=203593156286

但是,这个链接已经过时了,而应该是

https://console.developers.google.com/apis/api/bigquery-json.googleapis.com/overview?project=203593156286

谢谢。

python google-bigquery google-cloud-platform
2个回答
1
投票

尝试这样的东西,它会有所帮助(我在pyhton 2.7中使用它作为auth):

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/path_to_key/xxx.json"

credentials = GoogleCredentials.get_application_default()

bigquery_service = build('bigquery', 'v2', credentials=credentials)

此外,我在您的代码中没有看到your_project_id的任何定义。例如,我正在使用的查询:

query_response = query_request.query(
        projectId=your_project_id,
        body=query_data).execute()

1
投票

在执行Python代码的机器中,是否启用了“访问云API”?

在创建VM时,您可以更改它:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.