0

I'm struggling to trigger a Google Apps Script Function which builds many dropdowns of a Google Spreadsheet.

I need this data to be updated daily on this Google Spreadsheet so I must call the function.

Seems like I'm required to have a client-side authentication which I'm reluctant to force it to happen.

I have set already the Google API PHP Client which does batch updates, get requests and control users and its roles.

Here's how the code looks like:

public function updateTemplate ()
{
    $client     = $this->getClient();
    $service    = new Google_Service_Script($client);

    $scriptId = $this->gScriptId;

    // Create an execution request object.
    $request = new Google_Service_Script_ExecutionRequest();

    $request->setFunction('SetUpNewSpreadsheet');

    try {
        // Make the API request.
        $response = $service->scripts->run($scriptId, $request);

        echo "here<pre>";
        print_r($response);

        exit;

I'm getting the following response error:

Caught exception: { 
    "error": {  
        "code": 401, 
        "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.", 
        "errors": [ { 
            "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.", 
            "domain": "global", 
            "reason": "unauthorized" 
        } ]
        , "status": "UNAUTHENTICATED" 
    } 
}

I've checked the message errors and docs already. I'm wondering if maybe I'm missing something or if I can find a way to execute that request without forcing a client-side login?

Has anyone been through it?

Diego
  • 9,261
  • 2
  • 19
  • 33
Bruno FG
  • 125
  • 6
  • Have you looked at the [Google Apps Script API](https://developers.google.com/apps-script/api/how-tos/execute)? – Diego Apr 11 '18 at 05:31
  • @Diego, yes, it's using the Google Apps Script API. Here's what I'm trying to do https://developers.google.com/apps-script/api/how-tos/execute. It's a common Cloud Platform project (Google Apps Script and Google API PHP Client). – Bruno FG Apr 11 '18 at 05:43
  • Hey Bruno, take a look at this example and it might help you https://stackoverflow.com/questions/45422881/how-to-insert-a-column-in-google-spreadsheet/45474279#45474279 – Morfinismo Apr 11 '18 at 12:23
  • @Morfinismo I went through your post and pretty much we are doing the same. All settings on the console are equal. I found a solution for the credentials error, first I tried to deploy a new version and it didn't work. I ended up creating an entirely new Google Apps Script and it passed the credentials step and now it's saying - Script error message: Script function not found: SetUpNewSpreadsheet. The function is there and the ID is correct, I also tried a different function. Any ideas? – Bruno FG Apr 12 '18 at 04:55
  • Okey, I know the issue now but still not sure how I can find a workaround. Basically, if I execute a basic function, it does work. The function I'm trying to execute connects to an external API (our backend), retrieve data and builds the dropdowns and many other functionalities. It might be a scope thing or a client-side authentication which is needed when executing any of these functions as a user. Anyone? – Bruno FG Apr 12 '18 at 09:20

0 Answers0