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

我无法在Python 3上使用gspread进行授权,python,google-api,google-sheets-api,google-api-python-client,service-accounts,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/

我有一个在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, 第58行,在B64编码中 encoded=binascii.b2a\u base64s,newline=False TypeError:需要类似字节的对象,而不是“str”

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

credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'].encode(), scope)
但是现在,将SignedJwtAssertionCredentials移动到oauth2client.service_account.ServiceAccountCredentials之后,它就不起作用了


也许有人能帮我

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

from google.oauth2 import service_account
import googleapiclient.discovery

SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
SERVICE_ACCOUNT_FILE = '/path/to/service.json'

credentials = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)
service = build('sheets', 'v4', http=creds.authorize(Http()))

# Call the Sheets API
SPREADSHEET_ID = '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms'
RANGE_NAME = 'Class Data!A2:E'
result = service.spreadsheets().values().get(spreadsheetId=SPREADSHEET_ID,
                                            range=RANGE_NAME).execute()
values = result.get('values', [])

我又写了一次代码:credentials=service\u account.credentials.from\u service\u account\u file service\u account\u file,scopes=scopes service=apiclient.discovery.build'sheets',v4',credentials=credentials,但仍不能处理相同的错误。哪里出错了?听起来您的凭据文件有问题。您是否尝试过遵循文档链接?是的,我创建了服务帐户并获得JSON格式的凭据文件。它看起来是这样的:{type:service\u acc,project\u id:docs,private\u key\u id:sdadasd2211312312 private\u key:--开始-结束私钥,客户端\u电子邮件:sdsdsdsds@.iam.gserviceaccount.com,客户端id:1223211身份验证uri:,令牌uri:,身份验证提供者身份验证证书url:,客户端身份验证证书url:}您可以直接在gspread.authorizecredentials.json中使用从Google下载的凭证文件