我想从datalab连接到bigQuery并执行更新命令。 我为API&运行以下代码认证
from google.cloud import bigquery
# Get everything we possibly can from the service account JSON file
#set GOOGLE_APPLICATION_CREDENTIALS
cred = bigquery.Client.from_service_account_json('OrielResearch-da46e752c7ff.json')
# Instantiates a client
client = bigquery.Client(project='speedy-emissary-167213',credentials=cred)
# The name of the dataset
dataset_name = 'pgp_orielresearch'
# The name of the table
table_name = 'update_queries'
# Perform a synchronous query.
QUERY = (
'SELECT * FROM [speedy-emissary-167213:pgp_orielresearch.update_queries]')
query = client.run_sync_query(QUERY)
dataset = client.dataset(dataset_name)
tables, token = dataset.list_tables()
并收到以下错误: AttributeError:'Client'对象没有属性'authorize'
任何想法?
完整的堆栈是:
AttributeErrorTraceback (most recent call last)
<ipython-input-2-616f54fa35ba> in <module>()
19 query = client.run_sync_query(QUERY)
20 dataset = client.dataset(dataset_name)
---> 21 t = dataset.list_tables()
22 #query.timeout_ms = TIMEOUT_MS
23 #query.run()
/usr/local/lib/python2.7/dist-packages/google/cloud/bigquery/dataset.py in list_tables(self, max_results, page_token)
568 connection = self._client.connection
569 resp = connection.api_request(method='GET', path=path,
--> 570 query_params=params)
571 tables = [Table.from_api_repr(resource, self)
572 for resource in resp.get('tables', ())]
/usr/local/lib/python2.7/dist-packages/google/cloud/connection.pyc in api_request(self, method, path, query_params, data, content_type, api_base_url, api_version, expect_json, _target_object)
344 response, content = self._make_request(
345 method=method, url=url, data=data, content_type=content_type,
--> 346 target_object=_target_object)
347
348 if not 200 <= response.status < 300:
/usr/local/lib/python2.7/dist-packages/google/cloud/connection.pyc in _make_request(self, method, url, data, content_type, headers, target_object)
242 headers['User-Agent'] = self.USER_AGENT
243
--> 244 return self._do_request(method, url, headers, data, target_object)
245
246 def _do_request(self, method, url, headers, data,
/usr/local/lib/python2.7/dist-packages/google/cloud/connection.pyc in _do_request(self, method, url, headers, data, target_object)
270 :returns: The HTTP response object and the content of the response.
271 """
--> 272 return self.http.request(uri=url, method=method, headers=headers,
273 body=data)
274
/usr/local/lib/python2.7/dist-packages/google/cloud/connection.pyc in http(self)
101 self._http = httplib2.Http()
102 if self._credentials:
--> 103 self._http = self._credentials.authorize(self._http)
104 return self._http
105
AttributeError: 'Client' object has no attribute 'authorize'
答案 0 :(得分:1)
尝试像这样设置凭据:
import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'OrielResearch-da46e752c7ff.json'
from google.cloud.bigquery.client import Client
client = Client()