403使用Python写入Google工作表时缺少作用域时出错

403使用Python写入Google工作表时缺少作用域时出错,python,jupyter,Python,Jupyter,我正在尝试连接Python脚本以将输出上载到GSpread。我的回答中出现了403个错误: HttpError:https://www.googleapis.com/drive/v3/files/root?fields=id&alt=json 返回“权限不足:请求的身份验证作用域不足。”> MyCode如下所示: scope = ['https://www.googleapis.com/auth/spreadsheets'] credentials = ServiceAccountCredent

我正在尝试连接Python脚本以将输出上载到
GSpread
。我的回答中出现了403个错误:

HttpError:https://www.googleapis.com/drive/v3/files/root?fields=id&alt=json 返回“权限不足:请求的身份验证作用域不足。”>

MyCode如下所示:

scope = ['https://www.googleapis.com/auth/spreadsheets']
credentials = ServiceAccountCredentials.from_json_keyfile_name('xx-xxxxxx-xxxxx-xxxxxx.json', scope)


spreadsheet_key = 'XXXXXXXXXXXXXXXXXXXXXX' 

gc = gspread.authorize(credentials)
wks_name = 'Sheet1'

d2g.upload(predictions,spreadsheet_key,wks_name,credentials = credentials)

我对Python还很陌生,所以我正在边做边学,但我一辈子都无法理解这一点。非常感谢您的帮助

解决此问题的方法是添加对驱动器和电子表格的访问

scope = ['https://www.googleapis.com/auth/spreadsheets','https://www.googleapis.com/auth/drive']

例如,添加
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.readonly
?我将此作为评论发布,因为我不确定你的整个情况。如果这不能解决您的问题,我很抱歉。是的,添加驱动器API访问有效谢谢您的回复。我很高兴你的问题解决了。当您的问题得到解决时,您能否将其作为答案发布?这样,它将对其他有相同问题的用户有用。