2017-10-08 8 views
2

私は今日、私の新しいプロジェクトの1つにpython BigQuery APIを設定しようとしています。私は、BigQueryのAPIに接続するためのサービスアカウントを作成したBigQuery API BigQuery Python APIの問題

  • を有効にするプロジェクト
  • の課金を有効にする新規プロジェクト
  • を作成

    1. の全過程を経て

    最も簡単な例を試してみます

    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が有効になっている、次のエラーを取得し、私は問題が何であるかわかりません。私が困惑しているのは、私が他の2つのプロジェクトで同じプロセスを実行していることです。もう一つの観察。エラーが

    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コードを実行しているマシンでは、「クラウドAPIへのアクセス」は有効になっていますか?あなたはVMを作成しているとき

    あなたはそれを変更することができます。

    enter image description here