【问题标题】:RefreshError while trying to access spreadsheet from python尝试从 python 访问电子表格时出现 RefreshError
【发布时间】:2023-03-12 11:28:02
【问题描述】:

我试图按照this 教程从python 访问我的电子表格GetHookedTest。 我已成功添加服务帐户,这是我的凭据。通过工作表与 test@gethooked.iam.gserviceaccount.com

共享
{
  "type": "service_account",
  "project_id": "gethooked",
  "private_key_id": "aaaaaabbbbbaaaabbbbaaaaa",
  "private_key": "Private Key here",
  "client_email": "test@gethooked.iam.gserviceaccount.com",
  "client_id": "454454554",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/orderplacer%test.iam.gserviceaccount.com"
}

import gspread
from oauth2client.service_account import ServiceAccountCredentials
import pprint

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

creds = ServiceAccountCredentials.from_json_keyfile_name("client_secret.json", scope)
client = gspread.authorize(creds)
sheet = client.open("GetHookedTest").sheet1

但我收到 RefreshError

six.raise_from(new_exc, caught_exc)
  File "<string>", line 3, in raise_from
google.auth.exceptions.RefreshError: ('No access token in response.', {'id_token': 'long string'})

【问题讨论】:

  • 比如去掉https://spreadsheets.google.com/feedshttps://www.googleapis.com/auth/drive.file的作用域,用client.open_by_key("SpreadsheetId").sheet1代替client.open("GetHookedTest").sheet1,你会得到什么结果?
  • @Tanaike 谢谢它的工作,spreadsheets 也有错字
  • 感谢您的回复。我没注意到。但我很高兴你的问题得到了解决。如果您的问题已解决,您可以将其发布为答案吗?这样,它对遇到相同问题的其他用户很有用。
  • 谢谢。我将其发布为答案。你能确认一下吗?

标签: python google-sheets google-cloud-platform gspread oauth2client


【解决方案1】:

作为一种方法,请尝试以下修改。

  1. 删除https://spreadsheets.google.com/feedshttps://www.googleapis.com/auth/drive.file的范围。

  2. 使用电子表格 ID 而不是电子表格名称。

    • 来自

        client.open("GetHookedTest").sheet1
      
    •   client.open_by_key("SpreadsheetId").sheet1
      

注意:

  • 来自your reply commenthttps://www.googleapis.com/auth/spreasheetshttps://www.googleapis.com/auth/spreadsheets

【讨论】:

    最近更新 更多