python - Google API (Sheets) API 错误代码 403。权限不足:请求的身份验证范围不足
问题描述
我正在尝试一个项目,我可以使用 python(我在 Anaconda 上使用 jupyter 笔记本)从谷歌表格中读取数据。我观看了一些视频和指南并复制了代码。但是,我无法让代码正常工作
import pandas as pd
import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('test.json',scope)
client = gspread.authorize(creds)
data = gc.open('TEST').sheet1
print(data.get_all_records())
我得到的错误信息是
APIError: {
"error": {
"errors": [
{
"domain": "global",
"reason": "insufficientPermissions",
"message": "Insufficient Permission: Request had insufficient authentication scopes."
}
],
"code": 403,
"message": "Insufficient Permission: Request had insufficient authentication scopes."
}
}
关于我应该做什么的任何建议?
解决方案
身份验证范围允许您的应用程序使用 OAuth2 身份验证对服务执行某些操作。使用 google API 时,请参阅Google API 范围表。
请参阅下图中的 Google Sheets API V4 相关范围。请确保不要提供可能影响应用程序安全性的额外权限。
请注意:由于您计划更改范围,请先删除token.json
在本地项目文件夹中创建的文件,然后再次允许访问您的应用程序。这将创建一个新token.json
文件。
推荐阅读
- keyboard - gtk 读取当前键盘状态
- c# - 数据绑定到 TextBox - 在代码中设置 Text 属性不会更新模型
- css - 侯以某种方式更改 CSS 布局,中间 div 到顶部?
- spring - 如果父映射中的键相同,Spring 如何管理 map-merge='true'?
- rust - 如何对 Option<&Path> 进行模式匹配?
- javascript - 为什么 setTimeout 中的 setState 不进行批处理?
- c# - Restsharp JSON 正文需要括号
- azure - Microsoft Graph 使用客户端凭据流(应用程序权限)和个人帐户发送邮件
- java - 图像文件路径未在 Spring MVC 视图中加载
- group-by - 如何在普罗米修斯中做仪表度量的平均值