Python 2.7 如何使用Python与BigQuery建立连接

Python 2.7 如何使用Python与BigQuery建立连接,python-2.7,google-bigquery,Python 2.7,Google Bigquery,我是python新手,我使用过这段代码 from google.cloud import bigquery from google.cloud.bigquery import SchemaField client = bigquery.Client(project='xxxxxxxxxxxxx') 我发现了这个错误 C:\Python27\python.exe "C:/Users/Vikas Chauhan/python_programs/bigQuery.py" Traceback (

我是python新手,我使用过这段代码

from google.cloud import bigquery
from google.cloud.bigquery import  SchemaField


client = bigquery.Client(project='xxxxxxxxxxxxx')
我发现了这个错误

C:\Python27\python.exe "C:/Users/Vikas Chauhan/python_programs/bigQuery.py"
Traceback (most recent call last):
  File "C:/Users/Vikas Chauhan/python_programs/bigQuery.py", line 5, in <module>
    client = bigquery.Client(project='stellar-display-145814')
  File "C:\Python27\lib\site-packages\google\cloud\bigquery\client.py", line 83, in __init__
    project=project, credentials=credentials, _http=_http)
  File "C:\Python27\lib\site-packages\google\cloud\client.py", line 212, in __init__
    Client.__init__(self, credentials=credentials, _http=_http)
  File "C:\Python27\lib\site-packages\google\cloud\client.py", line 128, in __init__
    credentials = get_credentials()
  File "C:\Python27\lib\site-packages\google\cloud\credentials.py", line 39, in get_credentials
    credentials, _ = google.auth.default()
  File "C:\Python27\lib\site-packages\google\auth\_default.py", line 282, in default
    raise exceptions.DefaultCredentialsError(_HELP_MESSAGE)
google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or
explicitly create credential and re-run the application. For more
information, please see
https://developers.google.com/accounts/docs/application-default-credentials.
C:\Python27\python.exe“C:/Users/Vikas Chauhan/python\u programs/bigQuery.py”
回溯(最近一次呼叫最后一次):
文件“C:/Users/Vikas Chauhan/python_programs/bigQuery.py”,第5行,在
client=bigquery.client(project='stellar-display-145814')
文件“C:\Python27\lib\site packages\google\cloud\bigquery\client.py”,第83行,在\uuu init中__
项目=项目,凭证=凭证,\u http=\u http)
文件“C:\Python27\lib\site packages\google\cloud\client.py”,第212行,在\uuu init中__
客户端。uuu init_uuuu(self,凭证=凭证,http=http)
文件“C:\Python27\lib\site packages\google\cloud\client.py”,第128行,在\uuu init中__
凭证=获取凭证()
文件“C:\Python27\lib\site packages\google\cloud\credentials.py”,第39行,在get\u credentials中
凭据,uz=google.auth.default()
文件“C:\Python27\lib\site packages\google\auth\\u default.py”,默认为第282行
引发异常。DefaultCredentialsError(\u帮助\u消息)
google.auth.exceptions.DefaultCredentialsError:无法自动确定凭据。请设置GOOGLE\u应用程序\u凭据或
显式创建凭据并重新运行应用程序。更多
信息,请参阅
https://developers.google.com/accounts/docs/application-default-credentials.
我已经设置了环境变量。我一次又一次地犯这个错误

我是python新手,请帮助我解决这个问题。我对此没有任何知识,即使用python与bigquery建立连接。
谢谢

首先,您需要一个的python包。您可以使用pip安装它

pip install --upgrade google-api-python-client
然后设置环境变量,该变量指向您的服务帐户凭据。如果您没有服务帐户,请创建一个新的服务帐户,并将json凭据下载到您的本地帐户

GOOGLE_APPLICATION_CREDENTIALS=/path/to/credentials.json
现在一切都准备好了,只需编写一个python代码

from apiclient import discovery
from oauth2client.client import GoogleCredentials

credentials = GoogleCredentials.get_application_default()
bq_client = discovery.build('bigquery', 'v2', credentials=credentials)

发现BigQuery API和Google API浏览器。

谢谢它的使用。但是是否有任何方法可以使用OAuthURL进行连接。