【发布时间】:2018-09-03 11:39:03
【问题描述】:
我有一个在 Python 3.6 上创建 googlesheet 的简单脚本,例如:
import gspread
from oauth2client.service_account import ServiceAccountCredentials
mail = 'my_mail@gmail.com'
secret_file = "C:\\Users\\admin\\Downloads\\my_file.json"
SCOPES = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name(secret_file, SCOPES)
gss_client = gspread.authorize(credentials)
gss = gss_client.open('test-doc')
worksheet = gss.sheet1
但是当我运行它时,我有一个类型错误:
Traceback(最近一次调用最后一次):文件 “C:/Users/admin/PycharmProjects/untitled/fjhdshfjs.py”,第 11 行,在 ... "C:\Users\admin\AppData\Local\Programs\Python\Python36\lib\base64.py", 第 58 行,在 b64 编码中 encoded = binascii.b2a_base64(s, newline=False) TypeError: a bytes-like object is required, not 'str'
早期,这个问题可以通过编码解决:
credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'].encode(), scope)
但是现在,将 SignedJwtAssertionCredentials 移动到 oauth2client.service_account.ServiceAccountCredentials 上后它不起作用
也许有人可以帮助我?
【问题讨论】:
-
您可以直接在
gspread.authorize("credentials.json")使用从Google下载的凭证文件
标签: python google-api google-sheets-api google-api-python-client service-accounts