404在Python中使用应用程序脚本执行API时出错

404在Python中使用应用程序脚本执行API时出错,python,google-apps-script,oauth-2.0,google-api,google-apps-script-api,Python,Google Apps Script,Oauth 2.0,Google Api,Google Apps Script Api,在尝试使用应用程序脚本执行API时,我一直遇到404错误。我已经确保项目ID链接正确,并且打开了正确的API 这是我得到的回溯: Traceback (most recent call last): File "", line 70, in main response = service.scripts().run(body=request, scriptId=SCRIPT_ID).execute() File "/oauth2client/util.py", line 137,

在尝试使用应用程序脚本执行API时,我一直遇到404错误。我已经确保项目ID链接正确,并且打开了正确的API

这是我得到的回溯:

Traceback (most recent call last):
  File "", line 70, in main
    response = service.scripts().run(body=request, scriptId=SCRIPT_ID).execute()
  File "/oauth2client/util.py", line 137, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "apiclient/http.py", line 723, in execute
    raise HttpError(resp, content, uri=self.uri)
apiclient.errors.HttpError: <HttpError 404 when requesting https://script.googleapis.com/v1/scripts/ASFDQETWRQWEFAS12341234:run?alt=json returned "Requested entity was not found.">

看起来您必须转到脚本项目,
发布>作为API可执行文件部署
,然后选择有权访问脚本的
-
任何人

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

SCRIPT_ID = 'ASFDQETWRQWEFAS12341234'

CLIENT_SECRET = '/file/path'

def get_credentials():
  with open(CLIENT_SECRET, 'r') as f:
    content = json.load(f)
  credentials = client.SignedJwtAssertionCredentials(
      content['client_email'], content['private_key'].encode(), SCOPES)
  if hasattr(credentials, 'access_token'):
    if (hasattr(credentials, 'access_token_expired') and
        credentials.access_token_expired):
      http = httplib2.Http()
      credentials.refresh(http)
  return credentials

def main(unused_argv):
  credentials = get_credentials()
  http = credentials.authorize(httplib2.Http())
  service = build('script', 'v1', http=http)
  request = {
             'function': 'test',
             'devMode': True
            }
  response = service.scripts().run(body=request, scriptId=SCRIPT_ID).execute()
  if 'error' in response:
    raise AppsScriptError(response['error'])
  try:
    assert str(response['response']['result']) == 'Success!'
  except:
    raise AppsScriptError('Apps Script didn\'t complete successfully.')