【问题标题】:Google Sheet Api write errorGoogle Sheet Api 写入错误
【发布时间】:2018-08-09 16:34:55
【问题描述】:

我无法通过 python 将数据写入谷歌表格。我在 Google Sheets Api 示例中做了所有的事情,但它仍然不起作用。这是我的项目:

from __future__ import print_function
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file as oauth_file, client, tools

SCOPES = ['https://www.googleapis.com/auth/spreadsheets','https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/drive.file']

SAMPLE_SPREADSHEET_ID = 'ID'
SAMPLE_RANGE = 'Sheet2!A1:A10'

def main():
    store = oauth_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()))

values = {'values': [['one','two','three']]}
result = service.spreadsheets().values().append(
    spreadsheetId=SAMPLE_SPREADSHEET_ID, range=SAMPLE_RANGE,
    valueInputOption='RAW',
    body=values).execute()
print('{0} cells updated.'.format(result.get('updatedCells')));

if __name__ == '__main__':
    main()

它给了我这个错误:

C:\Users\Victor\Anaconda3\python.exe "C:/Users/Victor/Desktop/NEW try/try1.py"
Traceback (most recent call last):
File "C:/Users/Victor/Desktop/NEW try/try1.py", line 31, in <module>
    main()
    File "C:/Users/Victor/Desktop/NEW try/try1.py", line 27, in main
    body=values).execute()
    File "C:\Users\Victor\Anaconda3\lib\site-packages\googleapiclient\_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
File "C:\Users\Victor\Anaconda3\lib\site-packages\googleapiclient\http.py", line 842, in execute
    raise HttpError(resp, content, uri=self.uri)
    googleapiclient.errors.HttpError: <HttpError 403 when requesting https://sheets.googleapis.com/v4/spreadsheets/1RaeZ4QpT3-ZCcIfBDKdtQJt0WDJKiLnBsB3dTC2PoGg/values/Sheet2%21A1%3AA10:append?valueInputOption=RAW&alt=json returned "Request had insufficient authentication scopes.">

    Process finished with exit code 1

我固定了一个截图,证明我在 api 中拥有所有权限。 Google API 提前谢谢你。

【问题讨论】:

  • 我认为从您的情况来看,您脚本中的范围可能不会反映到“token.json”中的刷新令牌中。那么这个流量呢? 1、删除“token.json”。 2. 运行脚本,再次授权。由此,创建了包括反映范围的刷新令牌的“token.json”。如果这不是您的解决方案,我很抱歉。
  • 谢谢 Tanaike 成功了!
  • 欢迎。我很高兴你的问题得到了解决。感谢您尝试并告诉我。

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


【解决方案1】:

据我了解,退出代码 1 "means there was some issue / problem which caused the program to exit." 尝试调试您的代码,您的缩进似乎已关闭。第 11 行和第 25 行之间的所有内容都应该至少有一个缩进,因此它被认为是 main() 函数的一部分。像这样:

from __future__ import print_function
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file as oauth_file, client, tools

SCOPES = ['https://www.googleapis.com/auth/spreadsheets','https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/drive.file']

SAMPLE_SPREADSHEET_ID = 'ID'
SAMPLE_RANGE = 'Sheet2!A1:A10'

def main():
    store = oauth_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()))

    values = {'values': [['one','two','three']]}
    result = service.spreadsheets().values().append(
        spreadsheetId=SAMPLE_SPREADSHEET_ID, range=SAMPLE_RANGE,
        valueInputOption='RAW',
        body=values).execute()
    print('{0} cells updated.'.format(result.get('updatedCells')));

if __name__ == '__main__':
    main()

另外,Python 的 Google Sheets API 快速入门会经常更新,所以我会尝试使用新代码 featured here。让我们知道在您修复缩进或决定使用 Google 的更新代码后会发生什么。

【讨论】:

  • 嗨,乔丹。感谢您的回答,但没有帮助,它向我显示了同样的错误。我再次检查了错误,它显示我有错误 403,身份验证不足,而在 API 仪表板中它显示我可以完全访问所有资源。
猜你喜欢
  • 1970-01-01
  • 2016-09-27
  • 2017-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-22
  • 1970-01-01
  • 2017-02-03
相关资源
最近更新 更多