【问题标题】:Accessing Google Sheets Api with Python使用 Python 访问 Google Sheets Api
【发布时间】:2019-05-10 20:05:56
【问题描述】:

我目前正在尝试通过 python 更新谷歌表格,但我遇到了一些权限问题。

我一直按照 Twilio 指南中的说明进行操作:https://www.twilio.com/blog/2017/02/an-easy-way-to-read-and-write-to-a-google-spreadsheet-in-python.html

我在 Jupyter 中完成了所有这些工作,并且我确实将 JSON 文件保存到了正确的代码目录中。我在定义范围、信誉和客户方面没有任何问题。

import gspread
from oauth2client.service_account import ServiceAccountCredentials

scope = ['https://spreadsheets.google.com/feeds']
creds = ServiceAccountCredentials.from_json_keyfile_name('client_id.json', scope)
client = gspread.authorize(creds)

sheet = client.open("MixIQ Tracker").sheet1

我已按照所有步骤链接这两者,但最后一行出现此 API 错误。

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."
 }
}

我不确定如何解决这个问题。任何方向将不胜感激!

【问题讨论】:

标签: python google-sheets-api


【解决方案1】:

我相信 google drive API 端点需要包含在您的范围内。我正在将 Mailchimp API 中的数据写入 Google Sheet。

看看这个:https://www.youtube.com/watch?v=7I2s81TsCnc>这对我很有帮助。

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

【讨论】:

  • 更新该范围并启用 google sheet API 帮助了我
  • 用第二个任期更新范围也帮助我解决了这个问题
【解决方案2】:

如果您查看google API scopes documentation,您正在使用的范围 url 没有在任何地方引用。这可能是问题所在。尝试将范围 url 更改为 https://www.googleapis.com/auth/spreadsheets

此外,请确保在您的项目中的 Google 开发者控制台中正确启用了电子表格 API。

或者,您可以尝试Sheetfu library(我是作者),它会为您处理范围。

【讨论】:

    【解决方案3】:

    将此用作您的范围:

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

    在调用“ServiceAccountCredentials”之前

    您需要在“console.cloud.google”以及表格 API 中启用 Google Drive API。

    【讨论】:

      【解决方案4】:

      你只需要这个范围

      scope = ["https://www.googleapis.com/auth/drive"]
      

      【讨论】: