【问题标题】:Google Sheets API with Python带有 Python 的 Google 表格 API
【发布时间】:2021-11-09 21:19:03
【问题描述】:

我正在尝试连接到此 Google 表格: https://docs.google.com/spreadsheets/d/1u3Ql_Rs19PEe0mwqpsiw2wf2cUEbnSG4vZL63rh_OaE/edit?usp=sharing

.json 文件和 .py 文件都保存到我的桌面。

.json 中的电子邮件已与 Google 表格共享

我收到“JSONDecodeError: Expecting value”,但无法弄清楚原因。

我已经修改了 .json 中的客户端 ID 和密钥信息以防盗版,因为我是新手,不确定分享这是否明智

非常感谢任何帮助。谢谢你

蟒蛇:

import gspread

gc = gspread.service_account(filename = 'credentials.json')

sh = gc.open('User')

worksheet=sh.sheet1

res=worksheet.get_all_records()

print(res)

凭据.json:

{
  "type": "service_account",
  "project_id": "clear-heaven-331616",
  "private_key_id": "abcxyz",
  "private_key": "-----BEGIN PRIVATE KEY-----\abcxyz=\n-----END PRIVATE KEY-----\n",
  "client_email": "my-service-account@clear-heaven.iam.gserviceaccount.com",
  "client_id": "abcxyz",
  "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/my-service-account%40clear-heaven-331616.iam.gserviceaccount.com"
}

追溯:

  File "C:\Users\dusti\Desktop\googlesheets.py", line 10, in <module>
    gc = gspread.service_account(filename = 'credentials.json')

  File "C:\Users\dusti\anaconda3\lib\site-packages\gspread\auth.py", line 196, in service_account
    creds = ServiceAccountCredentials.from_service_account_file(

  File "C:\Users\dusti\anaconda3\lib\site-packages\google\oauth2\service_account.py", line 238, in from_service_account_file
    info, signer = _service_account_info.from_filename(

  File "C:\Users\dusti\anaconda3\lib\site-packages\google\auth\_service_account_info.py", line 73, in from_filename
    data = json.load(json_file)

  File "C:\Users\dusti\anaconda3\lib\json\__init__.py", line 293, in load
    return loads(fp.read(),

  File "C:\Users\dusti\anaconda3\lib\json\__init__.py", line 357, in loads
    return _default_decoder.decode(s)

  File "C:\Users\dusti\anaconda3\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())

  File "C:\Users\dusti\anaconda3\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None

JSONDecodeError: Expecting value

【问题讨论】:

  • 你在哪一行得到错误?粘贴完整的堆栈跟踪。另外,您是否尝试过改用open_by_key
  • 我确实尝试了 open_by_key 并得到了相同的结果。我已经更新了帖子以显示回溯(我相信这就是你所追求的?新来的......)。

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


【解决方案1】:

首先,我会仔细检查您是否在谷歌云控制台上创建了服务帐户凭据。

那我会考虑使用 Google-api-Python-client 库,这是 google 的官方客户端库。

那你应该考虑使用from_json_keyfile_name

"""Hello Sheets API V4."""

from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials


SCOPES = ['https://www.googleapis.com/auth/drive']
KEY_FILE_LOCATION = '<REPLACE_WITH_JSON_FILE>'
VIEW_ID = '<REPLACE_WITH_VIEW_ID>'


def initialize_sheets():
  """Initializes an sheets API V4 service object.

  Returns:
    An authorized sheets API V4 service object.
  """
  credentials = ServiceAccountCredentials.from_json_keyfile_name(
      KEY_FILE_LOCATION, SCOPES)

  # Build the service object.
  sheets = build('sheets', 'v4', credentials=credentials)

  return sheets

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-20
    • 2020-03-17
    • 2012-12-23
    相关资源
    最近更新 更多