首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Google Sheets API for python2.7 -->“无效的JSON有效负载。根元素必须是消息”

Google Sheets API for python2.7 -->“无效的JSON有效负载。根元素必须是消息”
EN

Stack Overflow用户
提问于 2018-08-21 01:45:42
回答 1查看 3.7K关注 0票数 6

几个星期以来,我一直在与这个错误作斗争,并尝试了之前发布的与Python API for Google Sheets相关的问题的解决方案。

当我通过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

任何帮助都很感谢--谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-21 06:53:55

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

发自:

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,并且您的访问令牌可用于Sheets

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51936017

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档