【问题标题】:Google API (Sheets) API Error code 403. Insufficient Permission: Request had insufficient authentication scopesGoogle API (Sheets) API 错误代码 403。权限不足:请求的身份验证范围不足
【发布时间】:2019-07-02 16:51:59
【问题描述】:

我正在尝试一个项目,我可以使用 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."
 }
}

对我应该做什么有什么建议吗?

【问题讨论】:

    标签: python google-sheets anaconda google-sheets-api


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      我在 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

      【讨论】:

        【解决方案3】:

        不要忘记允许访问您从 API 仪表板下载的凭据.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",
          .......
        }
        

        我的意思是,您应该转到您的 Google 表格文件并手动允许访问该电子邮件。这对我来说效果很好。

        【讨论】:

          【解决方案4】:

          这对我来说是这样的,例如从谷歌表中阅读文档

          import gspread
          from oauth2client.service_account import ServiceAccountCredentials
          # use creds to create a client to interact with the Google Drive API
          scope = ['https://spreadsheets.google.com/feeds',
                   'https://www.googleapis.com/auth/drive']
          creds = ServiceAccountCredentials.from_json_keyfile_name('my-drive.json', scope)
          client = gspread.authorize(creds)
          
          # Find a workbook by name and open the first sheet
          # Make sure you use the right name here.
          sheet = client.open("contactos-xxx").sheet1
          
          # Extract and print all of the values
          list_of_hashes = sheet.get_all_records()
          print(list_of_hashes)
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-04-28
            • 2023-03-13
            • 2019-07-06
            • 2021-12-12
            • 2019-10-02
            • 2021-11-22
            相关资源
            最近更新 更多