python - 带有 Python 的 Google 表格 API
问题描述
我正在尝试连接到此 Google 表格: https ://docs.google.com/spreadsheets/d/1u3Ql_Rs19PEe0mwqpsiw2wf2cUEbnSG4vZL63rh_OaE/edit?usp=sharing
.json 文件和 .py 文件都保存到我的桌面。
.json 中的电子邮件已与 Google 表格共享
我收到“JSONDecodeError:期望值”,但无法弄清楚原因。
我已经修改了 .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 库,这是 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
推荐阅读
- python - 如何在多个 Numpy 数组中获取相同值的位置
- python - 如何使用列表项的名称创建函数,
- html - 如何在聊天气泡外对齐用户名?
- android - android jetpack compose kotlin 合成扩展支持
- c++ - 是否有 C++ 库方法可以执行我编写的操作?
- r - 我可以在 R 中将 plotly() 与网络可视化一起使用吗?
- python - b64encode 带参数
- php - 如果产品类别有子项,则从 WooCommerce 中的父项中删除永久链接
- discord.js - Discord bot 如何让机器人删除文本频道?
- google-analytics - 来自多个类似来源的 PowerBI 在没有明显原因的情况下不匹配会话