gpt4 book ai didi

python - 谷歌电子表格 API 错误

转载 作者:行者123 更新时间:2023-11-28 22:00:46 27 4
gpt4 key购买 nike

我正在尝试使用 python 脚本访问 google 文档中的电子表格。但是在执行以下“标准”示例时,我总是会出错:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import time
import gdata.spreadsheet.service

if __name__ == "__main__":

email = 'email@gmail.com'
password = 'secret'
weight = '180'
# Find this value in the url with 'key=XXX' and copy XXX below
spreadsheet_key = '0AiKRVC_dEsdfafdC1Id2FGdGVMR1pHb3E4UGlmbUYxS1E'
# All spreadsheets have worksheets. I think worksheet #1 by default always
# has a value of 'od6'
worksheet_id = 'od6'

spr_client = gdata.spreadsheet.service.SpreadsheetsService()
spr_client.email = email
spr_client.password = password
spr_client.source = 'Example Spreadsheet Writing Application'
spr_client.ProgrammaticLogin()

# Prepare the dictionary to write
dict = {}
dict['date'] = time.strftime('%m/%d/%Y')
dict['time'] = time.strftime('%H:%M:%S')
dict['weight'] = weight
print dict

entry = spr_client.InsertRow(dict, spreadsheet_key, worksheet_id)
if isinstance(entry, gdata.spreadsheet.SpreadsheetsList):
print "Insert row succeeded."
else:
print "Insert row failed."

它根本不起作用。我总是得到以下错误

{'date': '01/07/2013', 'weight': '180', 'time': '19:58:44'}

Traceback (most recent call last):
File "/Path/to/Project//Python2GoogleDocs/src/P2GTest.py", line 31, in <module>
entry = spr_client.InsertRow(dict, spreadsheet_key, worksheet_id)
File "/Library/Python/2.7/site-packages/gdata/spreadsheet/service.py", line 339, in InsertRow
converter=gdata.spreadsheet.SpreadsheetsListFromString)
File "/Library/Python/2.7/site-packages/gdata/service.py", line 1236, in Post
media_source=media_source, converter=converter)
File "/Library/Python/2.7/site-packages/gdata/service.py", line 1358, in PostOrPut
'reason': server_response.reason, 'body': result_body}
gdata.service.RequestError: {'status': 400, 'body': 'We&#39;re sorry, a server error occurred. Please wait a bit and try reloading your spreadsheet.', 'reason': 'Bad Request'}

这里出了什么问题?电子邮件和密码是正确的,我从文档本身的 url 中提取了电子表格 key 。

最佳答案

确保字典中的列与电子表格中的列匹配。如果它们不在工作表中,您将收到错误消息。此外,虽然这不是您的问题,但您要确保字典键是小写的列名,所有空格都替换为 _。这是潜在错误的另一个来源(并且不止一次让我绊倒 :))。

关于python - 谷歌电子表格 API 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14202406/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com