Tsuyoshi Endo
Tsuyoshi Endo

Reputation: 737

TypeError: initial_value must be str or None, not bytes (bigquery)

I'm using python3.5 and I create bigquery client via bigquery using python ( https://github.com/tylertreat/BigQuery-Python )

Here is the code:

from bigquery import get_client

PROJECT_ID = 'api-project-xxxxxx'
SERVICE_ACCOUNT = '[email protected]'
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)

I got below error:

---------------------------------------------------------------------------
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

Upvotes: 0

Views: 910

Answers (1)

cs95
cs95

Reputation: 402912

StringIO converts a str object into a stream. You need to pass a str, but private_key is a bytes object, because you are reading the file in binary mode. Try this:

with open(PRIVATE_KEY_PATH, 'r') as f:
     private_key = f.read()

The rest of your code remains the same.

Upvotes: 1

Related Questions