Google 工作表 API "update" 方法 Http 错误 400

Google Sheets API "update" method Http Error 400

我正在尝试制作一个 python 脚本来读取和写入 google 传播sheet。我基本上已经在 https://developers.google.com/sheets/quickstart/python and modified it using the reference at https://developers.google.com/resources/api-libraries/documentation/sheets/v4/python/latest/ 复制了 python 快速启动脚本。

使用快速启动脚本中显示的 "get" 方法一切正常。我可以毫无错误地阅读 sheet。要使用 "update" 而不是 "get"(写入 sheet 而不是读取它),我删除了范围 url 的 .readonly 部分。我还将 get() 方法替换为 update() 并将 body 作为参数包含在 update() 方法中,其中 json 对象包含用 [=20 编码的值=].这都是根据上面的第二个参考。

我每次 "invalid data" 都会收到 HttpError 400。

代码:

import httplib2
import os
import json

from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools

try:
    import argparse
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/sheets.googleapis.com-python-quickstart.json
SCOPES = 'https://www.googleapis.com/auth/spreadsheets'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Google Sheets API Python Quickstart'


def get_credentials():
    """Gets valid user credentials from storage.

    If nothing has been stored, or if the stored credentials are invalid,
    the OAuth2 flow is completed to obtain the new credentials.

    Returns:
        Credentials, the obtained credential.
    """
    home_dir = os.path.expanduser('~')
    credential_dir = os.path.join(home_dir, '.credentials')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir,
                                   'sheets.googleapis.com-python-quickstart.json')

    store = oauth2client.file.Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else: # Needed only for compatibility with Python 2.6
            credentials = tools.run(flow, store)
        print('Storing credentials to ' + credential_path)
    return credentials

def main():
    """Shows basic usage of the Sheets API.

    Creates a Sheets API service object and prints the names and majors of
    students in a sample spreadsheet:
    https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
    """
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    discoveryUrl = ('https://sheets.googleapis.com/$discovery/rest?'
                    'version=v4')
    service = discovery.build('sheets', 'v4', http=http,
                              discoveryServiceUrl=discoveryUrl)

    spreadsheetId = '1Wbo5ilhw68IMUTSvnj_2yyRmWJ87NP-lHdJdaPBmTGA'
    rangeName = 'Class Data!A2:E'
    body = json.dumps({'values': [[0,0,0,0,0]]})
    result = service.spreadsheets().values().update(
        spreadsheetId=spreadsheetId, range=rangeName, body=body).execute()


if __name__ == '__main__':
    main()

控制台错误:

Traceback (most recent call last):
  File "/Users/user/Dropbox/python/jobs/test.py", line 73, in <module>
    main()
  File "/Users/user/Dropbox/python/jobs/test.py", line 69, in main
    spreadsheetId=spreadsheetId, range=rangeName, body=body).execute()
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/oauth2client/util.py", line 135, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/googleapiclient/http.py", line 832, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://sheets.googleapis.com/v4/spreadsheets/1Wbo5ilhw68IMUTSvnj_2yyRmWJ87NP-lHdJdaPBmTGA/values/Class%20Data%21A2%3AE?alt=json returned "Invalid value at 'data' (type.googleapis.com/google.apps.sheets.v4.ValueRange), "{"values": [[0, 0, 0, 0, 0]]}"">

我为自己设置了一个 google 开发帐户并遇到了一些挫折,但我能够管理。

我很确定 401 是由于提供的用于验证的 JSON 文件不正确(特别是如果您有多个项目并下载了多个 JSON 文件)

我还遇到了 403,表明我的 google 驱动器的 API 未启用:

确保您同时启用了 api(用于驱动器和电子表格)

大多数时候,在执行我的代码时,控制台会返回一个非常明确的错误并指向正确的方向:

raise HttpError(resp, content, uri=self.uri) googleapiclient.errors.HttpError: https://sheets.googleapis.com/v4/spreadsheets/DocumentID/values/Class%20Data%21A2%3AE?alt=json returned "Google Sheets API has not been used in project pytestapp before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/sheets.googleapis.com/overview?project=pytestapp then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.">

Process finished with exit code 1

我发现 update() 中包含的 body arg,虽然在文档中显示为 json,但实际上需要是一个常规的 python 字典。我在 json 中发送了一段文本,我猜 google api 客户端正在寻找的是一个实际的 python dict 对象。

我也是从 API 例子开始的。以上有关为云端硬盘和表格启用 API 的信息帮助我解决了访问错误。

然后我发现 .get(...).execute() returns 一个对象,它是 .update().execute() 中需要作为输入的类型,它看起来像:

{u'range': u'Sheet1!A1:B2', u'values': [[u'cella1', u'cellb1'],
[u'cella2', u'cellb2']], u'majorDimension': u'ROWS'}

在范围之间进行匹配,并将参数 valueInputOption='RAW' 添加到 .update() 后,我成功地写入了 Google sheet 使用此代码段:

myBody = {u'range': u'Sheet1!A1:B2', u'values': [[u'Zellea1', u'Zelleb1'], [u'Zellea2', u'Zelleb2']], u'majorDimension': u'ROWS'}
rangeOutput = 'Sheet1!A1:B2'
res = spreadsheet.values().update( spreadsheetId=spreadsheetId, range=rangeOutput, valueInputOption='RAW', body=myBody ).execute()

我遇到了同样的问题,但问题的根源不同。我发现有人删除了一个专栏。因此,我试图解析超出范围的列中的数据。

如果有人有同样的问题,我通过先在字典中获取列名+数字并根据列名解析数据来解决。

    headers = wks.get_row(2, include_tailing_empty=False)
    headers_dic={x:i for i, x in enumerate(headers)}

然后

    wks.update_col(headers_dic['Column Name']+1, data_as_list, row_offset=2)

offset=2 是因为 sheet 中 header 的类型,列中的 +1 是因为 python 和 sheet 开始它们的索引分别为 0 和 1。