Invalid JSON Payload error with python google sheets API

倾然丶 夕夏残阳落幕 提交于 2019-12-10 17:54:29

问题


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."

Here is my code:

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))

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


回答1:


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()


来源:https://stackoverflow.com/questions/44706584/invalid-json-payload-error-with-python-google-sheets-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!