3

I am trying to connect my Python script with my project in Google App Script. I have followed all the insctructions in this guide.

I have of course deployed it as an executable API and have tested it with access to only myself, my organization and anyone options.

When I pass the request with devMode as true, it all works fine. I understand that in this case, it is running the latest saved version. However when I set it to false then I get back the error "message": "Requested entity was not found." which as I understand is trying to run the latest deployed version.

I have also tried going through these questions 1 and 2 but apparently, the problem they had was the opposite where the script wouldn't run with devMode set to true.

Everything else seems to be executing correctly but I cannot find the reason why it wouldn't run the script without being on devMode

This is my script:

"""
Shows basic usage of the Apps Script API.
Call the Apps Script API to create a new script project, upload a file to the
project, and log the script's URL to the user.
"""
from __future__ import print_function
import pickle
import os.path
from googleapiclient import errors
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/script.projects',
          'https://www.googleapis.com/auth/spreadsheets.currentonly'
          ]

SCRIPT_ID = 'someid'


def main():
    """Calls the Apps Script API.
    """
    creds = None
    # The file token.pickle stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    service = build('script', 'v1', credentials=creds)

    # Call the Apps Script API
    try:
        # Create a new project
        request = {'function': 'setData'}
        response = service.scripts().run(body=request,
                                         scriptId=SCRIPT_ID).execute()

        print(response)
    except errors.HttpError as error:
        # The API encountered a problem.
        print(error.content)


if __name__ == '__main__':
    main()
everspader
  • 1,272
  • 14
  • 44
  • 1
    Could you share the script you are using? Also, have you checked all the scopes and all the configs? – Kessy Jan 18 '21 at 12:33
  • @Kessy I am using the script from the guide linked in my original post. Also, I have matched the scopes of the Script to those requested by the app script, so authentication doesn't seem to be a problem. It seems that the app is somehow not deployed as the URL given by the deploy windows gives and Error 404. – everspader Jan 18 '21 at 12:46
  • 1
    Have you recently change the IDE on apps script? could it be possible that the url may have changed. Have you tried doing a new deploy on apps script? – Kessy Jan 22 '21 at 15:47
  • @Kessy I haven't changed the IDE. And I tried deploying a few more times but it seems that it's not deployed. That's my guess because it can pick up the code in devMode = true which is the code saved and not the deployed – everspader Jan 22 '21 at 16:05
  • 2
    Have you managed to solve it? If not, have you checked that the parameters required on [https://developers.google.com/apps-script/api/how-tos/execute#the_scriptsrun_method](https://developers.google.com/apps-script/api/how-tos/execute#the_scriptsrun_method) are set properly? – Kessy Jan 27 '21 at 16:41
  • I haven't found a solution yet but I've checked the parameters and it all looks good. My script is exactly like the one on the guide linked on my original post apart from the project id and the function. – everspader Jan 28 '21 at 08:27
  • Have you checked that the script deployments are correct? Also, could you share the apps script code to reproduce it exactly? – Kessy Feb 04 '21 at 16:36
  • @Kessy I have checked and it seems correct to me but I assume that's where the problem is since it works with `devMode=True` meaning it's getting the latest version of the script saved instead of the deployed. I have updated the post with my code. – everspader Feb 08 '21 at 08:16
  • I may be lost here, but where are you setting the devMode? – Kessy Feb 11 '21 at 14:56
  • @Kessy this is the script that is not working. But I set: `request = {'function': 'setData', 'devMode': True}` and it all works fine. – everspader Feb 11 '21 at 16:00
  • Can you update on all the modifications and steps? – Kessy Feb 17 '21 at 15:47

1 Answers1

4

I have been able to face the same issue and I have found out that changing the script Id to the Deployment Id worked.

The deployment Id can be found on the Apps Script script:

  • Open the script
  • Go to Deploy -> Manage Deployments
  • Get the Deployment ID from the active deployment

Deployment ID

Once you have the deployment, go to the python script and modify the SCRIPT ID with the Deployment Id

Kessy
  • 1,894
  • 1
  • 8
  • 15