我无法在Python 3上使用gspread授权

时间:2018-09-03 11:39:04

标签: python google-api google-sheets-api google-api-python-client service-accounts

我有一个简单的脚本,可以在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

但是当我运行它时,出现类型错误:

  
    

回溯(最近通话最近):文件     “ C:/Users/admin/PycharmProjects/untitled/fjhdshfjs.py”,第11行,在      ...     “ C:\ Users \ admin \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ base64.py”,     b64encode中的第58行         编码= binascii.b2a_base64(s,newline = False)TypeError:需要一个类似字节的对象,而不是'str'

  

早期,可以通过编码解决此问题:

credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'].encode(), scope)

但是,现在,在将SignedJwtAssertionCredentials移至oauth2client.service_account.ServiceAccountCredentials

后,此方法不起作用

也许有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

我认为您所追求的代码更接近于此

toArray

Using OAuth 2.0 for Server to Server Applications