我正在使用python3.5并使用python(https://github.com/tylertreat/BigQuery-Python)通过bigquery创建bigquery client
以下是代码:
from bigquery import get_client
PROJECT_ID = 'api-project-xxxxxx'
SERVICE_ACCOUNT = 'xxxx@api-project-xxxx.iam.gserviceaccount.com'
PRIVATE_KEY_PATH = 'xxxx.p12'
with open(PRIVATE_KEY_PATH, 'rb') as f:
private_key = f.read()
client = get_client(PROJECT_ID,private_key=private_key,service_account=SERVICE_ACCOUNT,readonly=True)
我得到以下错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-181-858afcfa416a> in <module>()
----> 1 client = get_client(PROJECT_ID,private_key=private_key,service_account=SERVICE_ACCOUNT,readonly=True)
/Users/XXXXXX/.pyenv/versions/anaconda3-2.4.0/lib/python3.5/site-packages/bigquery/client.py in get_client(project_id, credentials, service_url, service_account, private_key, private_key_file, json_key, json_key_file, readonly, swallow_results)
131 credentials = _credentials().from_p12_keyfile_buffer(
132 service_account,
--> 133 StringIO(private_key),
134 scopes=scope)
135
TypeError: initial_value must be str or None, not bytes
答案 0 :(得分:1)
StringIO
将str
对象转换为流。您需要传递str
,但private_key
是一个字节对象,因为您正在以二进制模式读取文件。试试这个:
with open(PRIVATE_KEY_PATH, 'r') as f:
private_key = f.read()
其余代码保持不变。