2017-10-08 111 views
2

我整天都在試圖爲我的新項目之一設置python BigQuery API。我通過BigQuery Python 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

感謝。

回答

1

嘗試像此主機板這將有助於(我使用它的權威性在pyhton 2.7):

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代碼的機器中,是否啓用了「訪問Cloud API」?

當您創建一個虛擬機,你可以改變它:

enter image description here