【问题标题】:Google Sheets API for python2.7 --> "Invalid JSON payload. Root element must be a message"适用于 python2.7 的 Google Sheets API -->“无效的 JSON 有效负载。根元素必须是消息”
【发布时间】:2018-08-20 17:45:41
【问题描述】:

我已经为这个错误苦苦挣扎了好几个星期,并尝试了以前发布的与 Google Sheets 的 Python API 相关的问题的解决方案。

当我通过适用于 python 的 Google Sheets API 向我的电子表格发出“写入”请求时,我不断收到错误消息。该错误表明我提交了无效的 JSON,但我已经针对交互式测试窗口(Google APIs Explorer)测试了 JSON 结构,并且来自那里的请求正确更新了我的工作表。

代码如下

from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
import datetime
import json

# Call the Sheets API
SPREADSHEET_ID =  #mySheetID
RANGE_NAME = '2018_Raw Data!A3:A367'

months = { 0:"Jan", 1:"Feb",2:"Mar",4:"Apr",5:"May",6:"Jun",7:"Jul",8:"Aug",9:"Sep",10:"Oct",11:"Nov",12:"Dec"}


now = datetime.datetime.now()
date = str(now.day) +"-"+ months[now.month] + "-"+str(now.year)
day_of_year = now.timetuple().tm_yday
myRow = day_of_year+2

print (date)
print (myRow)


BWRange= '2018_Raw Data!B' + str(myRow)
BFRange= '2018_Raw Data!C' + str(myRow)
myBodyWeight=150
myBF="10%"
print (BWRange)
print (BFRange)


BWData = {}
BWData['values']= [[myBodyWeight]]
BWData['majorDimension']="ROWS"
BWData['range']= BWRange
BWJson= json.dumps(BWData)

BFData = {}
BFData['values']= [[myBF]]
BFData['majorDimension']="ROWS"
BFData['range']= BFRange
BFJson= json.dumps(BFData)

print (BWJson)
print (BFJson)


# Setup the Sheets API
SCOPES = 'https://www.googleapis.com/auth/spreadsheets'
store = file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
    creds = tools.run_flow(flow, store)
service = build('sheets', 'v4', http=creds.authorize(Http()))


#bw                              
request = service.spreadsheets().values().update(spreadsheetId=SPREADSHEET_ID,range=BWRange, valueInputOption="USER_ENTERED", body=BWJson)
response = request.execute()
pprint(response)

#bf
request = service.spreadsheets().values().update(spreadsheetId=SPREADSHEET_ID, range=BFRange,valueInputOption="USER_ENTERED", body=BFJson)
response = request.execute()
pprint(response)

错误如下:

Traceback (most recent call last):
  File "C:\sheets\mySheets.py", line 65, in <module>
    response = request.execute()
  File "C:\Python27\lib\site-packages\googleapiclient\_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "C:\Python27\lib\site-packages\googleapiclient\http.py", line 842, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://sheets.googleapis.com/v4/spreadsheets/1demD8sm5-Jvi7ImHcOu03sHaU7PF61ym1eyvjN1bGfw/values/2018_Raw%20Data%21B234?alt=json&valueInputOption=USER_ENTERED returned "Invalid JSON payload received. Unknown name "": Root element must be a message.">

我查看了以下帖子:
Python3 google spreadsheet api batchUpdate Json formatting
Invalid JSON Payload error with python google sheets API

任何帮助表示赞赏 - 谢谢!

【问题讨论】:

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


    【解决方案1】:

    我认为您的请求正文是正确的。那么这个修改怎么样呢?

    发件人:

    request = service.spreadsheets().values().update(spreadsheetId=SPREADSHEET_ID,range=BWRange, valueInputOption="USER_ENTERED", body=BWJson)
    
    request = service.spreadsheets().values().update(spreadsheetId=SPREADSHEET_ID, range=BFRange,valueInputOption="USER_ENTERED", body=BFJson)
    

    收件人:

    request = service.spreadsheets().values().update(spreadsheetId=SPREADSHEET_ID,range=BWRange, valueInputOption="USER_ENTERED", body=BWData)
    
    request = service.spreadsheets().values().update(spreadsheetId=SPREADSHEET_ID, range=BFRange,valueInputOption="USER_ENTERED", body=BFData)
    

    注意:

    • 在此修改中,json.dumps() 已被删除。
    • 此脚本假定在 API 控制台中启用了 Sheets API,并且您的访问令牌可用于电子表格().values().update()。

    如果这不起作用,请告诉我。我想修改它。

    【讨论】:

    • 干得好!!!您是对的 - 修改导致更新成功!我有点困惑,因为我的印象是 API 要求将正文作为 JSON 格式的字符串提交..?
    • @hsingarajah 很高兴您的问题得到了解决。也谢谢你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    • 1970-01-01
    • 2020-12-01
    • 2017-11-26
    • 2020-06-26
    • 2019-06-12
    • 2018-07-31
    相关资源
    最近更新 更多