Skip to main content
 首页 » 编程设计

python之Google App Script API 无法验证 "Request contains an invalid argument"

2024年11月24日47grandyang

我几乎是在使用来自谷歌自己网站的示例

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 方面的问题,因为即使他们的示例代码也不起作用。

我尝试创建一个新的 Google Cloud Platform 并获取一个新的 credentials.json 文件。我还尝试使用不同的电子邮件帐户进行身份验证。

请您参考如下方法:

我遇到了同样的错误。

我在 1 年多以前就使用过该代码。

但突然间我从 2 月 23 日起就无法使用了。

所以我采用了另一种方法,我使用了oauth2的post请求。

我认为您需要更新您的 Google 服务帐户。

我认为范围 ui 会发生一些变化...

祝你好运!

■python代码

from oauth2client import client 
 
def request_to_gas(): 
    credentials = client.OAuth2Credentials( 
        access_token=None, 
        client_id={your_client_id}, 
        client_secret={your_client_secret}, 
        refresh_token={your_refresh_token}, 
        token_expiry=None, 
        token_uri=GOOGLE_TOKEN_URI, 
        user_agent=None, 
        revoke_uri=GOOGLE_REVOKE_URI) 
 
    credentials.refresh(httplib2.Http())  # refresh the access token 
 
 
    my_url = "your_google_apps_script_web_url"  
    myheaders = {'Authorization': 'Bearer {}'.format(credentials.access_token), 
                 "Content-Type": "application/json" 
                 } 
    response = requests.post(my_url, 
                  data=json.dumps({ 
                                   'localdate' : '2019/02/23 12:12:12'}), 
                  headers=myheaders 
                  ) 

添加代码并发布为网络应用。

■谷歌应用脚​​本代码

function doPost(e) { 
  var params = JSON.parse(e.postData.getDataAsString());  // ※ 
  var value = params.localdate;  // get python code -- 2019/02/23 12:12:12 
 
  // here is your google apps script api 
  do_something(); 
 
  var output = ContentService.createTextOutput(); 
  output.setMimeType(ContentService.MimeType.JSON); 
  output.setContent(JSON.stringify({ message: "success!" })); 
 
  return output; 
}