【发布时间】:2019-02-23 04:30:46
【问题描述】:
我几乎在使用谷歌自己网站上的示例
https://developers.google.com/apps-script/api/how-tos/execute
示例python脚本的相关部分复制如下
from __future__ import print_function
from googleapiclient import errors
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file as oauth_file, client, tools
def main():
"""Runs the sample.
"""
SCRIPT_ID = 'ENTER_YOUR_SCRIPT_ID_HERE'
# Setup the Apps Script API
SCOPES = 'https://www.googleapis.com/auth/script.projects'
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('script', 'v1', http=creds.authorize(Http()))
if __name__ == '__main__':
main()
我收到以下错误
File "test.py", line 67, in <module>
main()
File "test.py", line 22, in main
service = build('script', 'v1', http=creds.authorize(Http()))
File "C:\Users\pedxs\Anaconda2\lib\site-packages\googleapiclient\_helpers.py", line 130, in positional_wrapper
return wrapped(*args, **kwargs)
File "C:\Users\pedxs\Anaconda2\lib\site-packages\googleapiclient\discovery.py", line 232, in build
raise e
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/discovery/v1/apis/script/v1/rest returned "Request contains an invalid argument.">
就在一周前,我有这个确切的代码工作。第 22 行使用发现构建功能,据我了解,它正在向 Google 的 API 身份验证器服务器“https://www.googleapis.com/discovery/v1/apis/script/v1/rest”发送凭据。我怀疑这是谷歌方面的问题,因为即使他们的示例代码也不起作用。
我已尝试创建新的 Google Cloud Platform 并获取新的 credentials.json 文件。我还尝试使用其他电子邮件帐户进行身份验证。
【问题讨论】:
-
我认为这可能对您的情况有用。 stackoverflow.com/questions/54818256/…
-
@Tanaike。那么这种身份验证方法似乎不再有效?您是否找到了另一种可行的方法?
-
有几种方法。 1.在Quickstart使用授权脚本。 2.如果您想在问题中使用脚本,请将
http = credentials.authorize(httplib2.Http())和service = discovery.build('script', 'v1', http=http)修改为service = build('script', 'v1', credentials=creds)。这在here 和here 进行了讨论。 -
我遇到的问题与原始问题完全相同。我的脚本已经运行了将近 2 年,现在我在没有进行其他更改的情况下遇到了同样的错误。谷歌方面有什么变化吗?
-
@Tanaike 您应该将其发布为答案
标签: python google-api-python-client oauth2client google-apps-script-api