[英]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.