我正在尝试一个项目,在这个项目中我可以使用python (我在Anaconda上使用jupyter笔记本)来读取google sheets中的数据。我看了一些视频和指南,并复制了代码。但是,我无法让代码正常工作
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."
}
}
对我应该做什么有什么建议吗?
发布于 2019-12-25 10:58:02
身份验证范围允许您的应用程序使用OAuth2身份验证在服务上执行某些操作。使用google API时,请参考Google API scopes sheet。
请参阅下面image.Please中与Google V4相关的作用域,确保不要授予额外的权限,这可能会影响应用程序的安全性。
请注意:由于您计划更改作用域,请首先删除在本地项目文件夹中创建的token.json
文件,然后再次允许访问您的应用程序。这将创建一个新的token.json
文件。
发布于 2020-04-22 01:42:04
我在github上看到了一种使用google lib的新方式。这样做
import gspread
from google.oauth2.service_account import Credentials
scope = ['https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive']
creds = Credentials.from_service_account_file('client_secret.json', scopes=scope)
client = gspread.authorize(creds)
我使用了这个链接:https://github.com/burnash/gspread
发布于 2019-09-01 07:26:18
不要忘记允许访问您从API dashboard下载的credentials.json中声明的电子邮件(client_email)的工作表:
{
"type": "service_account",
"project_id": "XXXXXXXXXXX",
"private_key_id": ".........",
"private_key": .........
"client_email": "getgooglesheets@xxxxxxxxxx.iam.gserviceaccount.com",
"client_id": "..............",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
.......
}
我的意思是,你应该去你的谷歌工作表文件,并手动允许访问这封电子邮件。这在我的情况下工作得很好。
https://stackoverflow.com/questions/56857018
复制相似问题