使用 Python 在 Google Sheets API v4 中写入
Writing in Google Sheets API v4 using Python
我一直在非常努力地通过阅读示例来使我的代码正常工作,但是我找不到关于使用 API 在 sheet 中正确编写的示例。
这是我的代码中不起作用的部分:
def comprar(producto,cantidad):
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 = '1oN1WkdXSYIlsgjFKZX1YolwajNflqAgE20w8vcRaY8Y'
row = searchInCol("A",producto)
target = "Inventario!"+"J"+str(row)
values = [
[
# Cell values to be inputed...
int(cantidad)
],
# Additional rows ...
]
body = {"values":values}
print("Entra")
#THIS ISN'T WOKRING
result = service.spreadsheets().values().update(
spreadsheetId=spreadsheetId, range=target,
valueInputOption="USER_ENTERED", body=body).execute()
print("entra")
无法使用的功能是比较功能,控制台显示:"Request had insufficient authentication scopes"。我什至不明白这是什么意思。
顺便说一句,你有函数 get_credentials():
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 = 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
这里我导入了所有模块:
from __future__ import print_function
import httplib2
import os
from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
import time
如果有人可以向我解释一些问题所在,以及使用 python 和 API 在 google sheet 中编写的工作代码示例,这真的很有帮助。而且,如果您需要有关代码的更多信息,请告诉我。 (我只是一个菜鸟学生。)
编辑:
我改变了这个:
SCOPES = 'https://www.googleapis.com/auth/spreadsheets.readonly'
为此:
SCOPES = 'https://www.googleapis.com/auth/spreadsheets'
但它不断抛出以下错误:
Traceback (most recent call last):
File "quickstart.py", line 210, in <module>
comprar(pro,cnt)
File "quickstart.py", line 196, in comprar
valueInputOption="USER_ENTERED", body=body).execute()
File "/usr/local/lib/python2.7/dist-packages/oauth2client/_helpers.py", line 133, in positional_wrapper
return wrapped(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/googleapiclient/http.py", line 838, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://sheets.googleapis.com/v4/spreadsheets/1oN1WkdXSYIlsgjFKZX1YolwajNflqAgE20w8vcRaY8Y/values/Inventario%21J1?alt=json&valueInputOption=USER_ENTERED returned "Request had insufficient authentication scopes.">
谢谢!
嗯,请确保您 启用您在此处使用的所有 API,尤其是 Sheets API 在您的开发者控制台中。
您收到的错误 "Request had insufficient authentication scopes"
是请求中提供的 OAuth 2.0 令牌中的一个错误,指定您正在使用的 scopes 不足以访问请求的数据。
因此请务必按照此处关于如何使用 OAuth 授权您的请求的documentation步骤进行操作。
更多信息可以查看这个相关的.
我一直在非常努力地通过阅读示例来使我的代码正常工作,但是我找不到关于使用 API 在 sheet 中正确编写的示例。
这是我的代码中不起作用的部分:
def comprar(producto,cantidad):
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 = '1oN1WkdXSYIlsgjFKZX1YolwajNflqAgE20w8vcRaY8Y'
row = searchInCol("A",producto)
target = "Inventario!"+"J"+str(row)
values = [
[
# Cell values to be inputed...
int(cantidad)
],
# Additional rows ...
]
body = {"values":values}
print("Entra")
#THIS ISN'T WOKRING
result = service.spreadsheets().values().update(
spreadsheetId=spreadsheetId, range=target,
valueInputOption="USER_ENTERED", body=body).execute()
print("entra")
无法使用的功能是比较功能,控制台显示:"Request had insufficient authentication scopes"。我什至不明白这是什么意思。 顺便说一句,你有函数 get_credentials():
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 = 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
这里我导入了所有模块:
from __future__ import print_function
import httplib2
import os
from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
import time
如果有人可以向我解释一些问题所在,以及使用 python 和 API 在 google sheet 中编写的工作代码示例,这真的很有帮助。而且,如果您需要有关代码的更多信息,请告诉我。 (我只是一个菜鸟学生。)
编辑: 我改变了这个:
SCOPES = 'https://www.googleapis.com/auth/spreadsheets.readonly'
为此:
SCOPES = 'https://www.googleapis.com/auth/spreadsheets'
但它不断抛出以下错误:
Traceback (most recent call last):
File "quickstart.py", line 210, in <module>
comprar(pro,cnt)
File "quickstart.py", line 196, in comprar
valueInputOption="USER_ENTERED", body=body).execute()
File "/usr/local/lib/python2.7/dist-packages/oauth2client/_helpers.py", line 133, in positional_wrapper
return wrapped(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/googleapiclient/http.py", line 838, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://sheets.googleapis.com/v4/spreadsheets/1oN1WkdXSYIlsgjFKZX1YolwajNflqAgE20w8vcRaY8Y/values/Inventario%21J1?alt=json&valueInputOption=USER_ENTERED returned "Request had insufficient authentication scopes.">
谢谢!
嗯,请确保您 启用您在此处使用的所有 API,尤其是 Sheets API 在您的开发者控制台中。
您收到的错误 "Request had insufficient authentication scopes"
是请求中提供的 OAuth 2.0 令牌中的一个错误,指定您正在使用的 scopes 不足以访问请求的数据。
因此请务必按照此处关于如何使用 OAuth 授权您的请求的documentation步骤进行操作。
更多信息可以查看这个相关的