簡體   English   中英

使用 Python 的 Google Sheets API

[英]Google Sheets API with Python

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

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

.json 中的電子郵件已與 Google Sheet 共享

我收到“JSONDecodeError: Expecting value”並且無法弄清楚原因。

我已經修改了 .json 中的客戶端 ID 和密鑰信息以進行盜版,因為我是新來的,並且不確定分享這是否明智

任何幫助是極大的贊賞。 謝謝你

Python:

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

首先,我會仔細檢查您是否在谷歌雲控制台上創建了服務帳戶憑據。

然后我會考慮使用 Google-api-Python-client 庫,這是 googles 官方客戶端庫。

那么你應該考慮使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM