2016-04-14 42 views
1

我正在在Python下面的代碼從數據實驗室筆記本連接到大的查詢API:無法連接至BigQuery API從DataLab

!pip install google-api-python-client==1.4.2 httplib2==0.9.2 oauth2client==1.5.2 pyasn1==0.1.9 pyasn1-modules==0.0.8 rsa==3.2.3 simplejson==3.8.1 six==1.10.0 uritemplate==0.6 wheel==0.24.0 

import httplib2 
import time 
import datetime as dt 
import sys 
import subprocess 
from googleapiclient import errors 
from googleapiclient.discovery import build 
from oauth2client.file import Storage 
from oauth2client.client import GoogleCredentials 
from googleapiclient.http import MediaFileUpload 
import gcp.bigquery as bq 
import pandas as pd 

credentials = GoogleCredentials.get_application_default() 

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

我收到以下錯誤:

ImportError Traceback (most recent call last) in()

----> 5 credentials = GoogleCredentials.get_application_default() 6 7 bigquery_service = build('bigquery', 'v2', credentials=credentials)

/usr/local/lib/python2.7/dist-packages/oauth2client/client.pyc in >get_application_default() 1202 """ 1203 return self -> 1204 1205 @property 1206 def serialization_data(self):

/usr/local/lib/python2.7/dist-packages/oauth2client/client.pyc in _get_implicit_credentials(cls) 1187 access_token, client_id, client_secret, refresh_token, 1188 token_expiry, token_uri, user_agent, revoke_uri=revoke_uri) -> 1189 1190 def create_scoped_required(self): 1191 """Whether this Credentials object is scopeless.

/usr/local/lib/python2.7/dist-packages/oauth2client/client.pyc in _implicit_credentials_from_gce() 1123 def _in_gce_environment(): 1124 """Detect if the code is running in the Compute Engine environment. -> 1125 1126 Returns: 1127 True if running in the GCE environment, False otherwise.

/usr/local/lib/python2.7/dist-packages/oauth2client/client.pyc in _get_application_default_credential_GCE() 1378 credentials: the credentials to be saved to the well known file; 1379 it should be an instance of GoogleCredentials -> 1380 well_known_file: the name of the file where the credentials are to be 1381 saved; this parameter is supposed to be used for 1382 testing only

/usr/local/lib/python2.7/dist-packages/oauth2client/gce.py in() 24 from oauth2client._helpers import _from_bytes 25 from oauth2client import util ---> 26 from oauth2client.client import HttpAccessTokenRefreshError 27 from oauth2client.client import AssertionCredentials 28

ImportError: cannot import name HttpAccessTokenRefreshError

我錯過了什麼?爲什麼這個谷歌提供的示例不適用於數據實驗室?

+0

爲什麼不只是使用Datalab中內置的BigQuery支持(在gcp.bigquery和受支持的%bigquery magics中)? –

回答

1

做一些google搜索後,我找到了答案在這裏:authentication error in Cloud Datalab: "ImportError: No module named gce」

該解決方案解決了我的問題。

+0

其他問題可能會解決您的問題,但您不需要爲BigQuery跳過那些箍環(因爲我嘗試構建/使用Genomics服務,所以我在該問題中所做的工作是必需的)。您可以在github上找到大量的datalab/bigquery示例,例如:https://github.com/GoogleCloudPlatform/datalab/tree/master/content/datalab/tutorials/BigQuery – SheRey