首页 > 解决方案 > 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."
 }
}

关于我应该做什么的任何建议?

标签: pythongoogle-sheetsanacondagoogle-sheets-api

解决方案


身份验证范围允许您的应用程序使用 OAuth2 身份验证对服务执行某些操作。使用 google API 时,请参阅Google API 范围表

请参阅下图中的 Google Sheets API V4 相关范围。请确保不要提供可能影响应用程序安全性的额外权限。

在此处输入图像描述

请注意:由于您计划更改范围,请先删除token.json在本地项目文件夹中创建的文件,然后再次允许访问您的应用程序。这将创建一个新token.json文件。


推荐阅读