Home » Php » php – Google App Script Api 404 error while running script via api

php – Google App Script Api 404 error while running script via api

Posted by: admin February 25, 2020 Leave a comment


i am currently trying to run an app script function when user connect there google account. when one row is finished typing i want the row information back to my server.so i came to a conclusion that app script is suitable for that.

what i have do step by step is

1) create a google console project

2) enable app script

3) enable google sheet api

4) download credentials.json

5) create a script

require __DIR__ . '/vendor/autoload.php';

if (php_sapi_name() != 'cli') {
    throw new Exception('This application must be run on the command line.');

 * Returns an authorized API client.
 * @return Google_Client the authorized client object
function getClient()

    $client = new Google_Client();
    $client->setApplicationName('Google Apps Script API PHP Quickstart');
    $client->setPrompt('select_account consent');

    // Load previously authorized token from a file, if it exists.
    // The file token.json stores the user's access and refresh tokens, and is
    // created automatically when the authorization flow completes for the first
    // time.
    $tokenPath = 'token.json';
    if (file_exists($tokenPath)) {
        $accessToken = json_decode(file_get_contents($tokenPath), true);

    // If there is no previous token or it's expired.
    if ($client->isAccessTokenExpired()) {
        // Refresh the token if possible, else fetch a new one.
        if ($client->getRefreshToken()) {
        } else {
            // Request authorization from the user.
            $authUrl = $client->createAuthUrl();
            printf("Open the following link in your browser:\n%s\n", $authUrl);
            print 'Enter verification code: ';
            $authCode = trim(fgets(STDIN));

            // Exchange authorization code for an access token.
            $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);

            // Check to see if there was an error.
            if (array_key_exists('error', $accessToken)) {
                throw new Exception(join(', ', $accessToken));
        // Save the token to a file.
        if (!file_exists(dirname($tokenPath))) {
            mkdir(dirname($tokenPath), 0700, true);
        file_put_contents($tokenPath, json_encode($client->getAccessToken()));
    return $client;

 * Shows basic usage of the Apps Script API.
 * Call the Apps Script API to create a new script project, upload files to the
 * project, and log the script's URL to the user.
$client = getClient();
$service = new Google_Service_Script($client);

// Create a management request object.
$request = new Google_Service_Script_CreateProjectRequest();
$request->setTitle('console script 44400');
$response = $service->projects->create($request);

$scriptId = $response->getScriptId();

$code = <<<EOT
function customFunction(e) {
  var range = e.range;
  range.setNote('Last modified: ' + new Date());
  var ui = SpreadsheetApp.getUi();

$file1 = new Google_Service_Script_ScriptFile();
$file1->setName('hello 111100');

$manifest = <<<EOT
  "timeZone": "America/New_York",
  "exceptionLogging": "CLOUD",
    "oauthScopes": [
$file2 = new Google_Service_Script_ScriptFile();

$request = new Google_Service_Script_Content();
$request->setFiles([$file1, $file2]);

$response = $service->projects->updateContent($scriptId, $request);
echo "https://script.google.com/d/" . $response->getScriptId() . "/edit\n";

  $request = new \Google_Service_Script_ExecutionRequest();
$response = $service->scripts->run($scriptId, $request);

I got the response as

  Fatal error: Uncaught Google_Service_Exception: {
  "error": {
    "code": 404,
    "message": "Requested entity was not found.",
    "errors": [
        "message": "Requested entity was not found.",
        "domain": "global",
        "reason": "notFound"
    "status": "NOT_FOUND"

the error is due to

$request = new \Google_Service_Script_ExecutionRequest();
$response = $service->scripts->run($scriptId, $request);

i see similar errors on internet .but the solutions don’t work for me. i pass all the necessary scopes


between as a side note when i try to run the script manually in the script editor and try editing in the google sheet the above script work and the alert box shown. but this is not practical in case of user who connected there google account.we cannot guarantee that user run script manually to make things work.we need the api to run the script and make the trigger working.Please help

version code added

$request1=new Google_Service_Script_Version();

added above script run API code but still get same error

How to&Answers: