本文介绍了python Google Sheets API的无效JSON Payload错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Google表格API将新行追加到现有的Google表格中.身份验证已经成功进行,并且代码能够读取工作表的内容.但是,当我尝试使用service.spreadsheets().values().append(...)时,出现以下HttpError:"Invalid JSON payload received. Unknown name "": Root element must be a message."

I'm trying to use the google sheets API to append new rows to an existing google sheet. Authentication already happens successfully and code is able to read the contents of the sheet.However, when I try to use service.spreadsheets().values().append(...), I get the following HttpError: "Invalid JSON payload received. Unknown name "": Root element must be a message."

这是我的代码:

from json import dumps as jsdumps
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
discoveryUrl = discoveryUrl = ('https://sheets.googleapis.com/$discovery/rest?version=v4')
service = discovery.build('sheets', 'v4', http=http, discoveryServiceUrl=discoveryUrl)

spreadsheetId = '{...}'
rangeName = "Sheet1"
value_input_option = 'RAW'
insert_data_option = 'INSERT_ROWS'
value_range_body = {
    "range": rangeName,
    "majorDimension:": "ROWS",
    "values": list(rd.values()), # rd is just a dictionary with some stuff in it
}

那么当我尝试发送它时:

So then when I try to send it:

request = service.spreadsheets().values().append(spreadsheetId=spreadsheetId, range=rangeName, valueInputOption=value_input_option, insertDataOption=insert_data_option, body=jsdumps(value_range_body))

运行request.execute()后,出现上面列出的错误.

After running request.execute(), I get the error listed above.

推荐答案

我遇到了类似的问题.这对我有用:

I was having a similar problem. This worked for me:

   spreadsheetId = 'foobar'
    range = 'Sheet1!A1'
    body = {
        'values': [data],
        "majorDimension:": "ROWS",

    }



    self.service.spreadsheets().values().append(spreadsheetId=spreadsheet_id, range=range,
                                                    valueInputOption='RAW',
                                                    insertDataOption='INSERT_ROWS',
                                                    body=body).execute()

这篇关于python Google Sheets API的无效JSON Payload错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 10:02