title: Calling a google apps script execution api from PHP using a service account
body: <p>I'm trying to call a very simple google apps script from php using a service account so that only my server can access it, not users of the website.</p>
  
  <p>Here is how I do. I create the script here <a href="https://script.google.com" rel="nofollow">https://script.google.com</a></p>
  
  <pre><code>function get() {
      return  ContentService.createTextOutput('get method');
  }
  </code></pre>
  
  <p>and a new project is automatically associated when i save it.
  Then I open <code>File &gt; Project Properties</code> to get the <code>scriptId = MM8zQqofS1OIcnFiNSlm1CGEl5qMrMVBt</code></p>
  
  <p>I access the developer console of the associated project threw <code>Resources &gt; Project Developers console</code> by clicking on the project link at the top of the popup displayed.</p>
  
  <p>Then I click 'Activate and manage API' and activate the 'Google Apps Script Execution API'. I click on 'Credentials' and see that the previous operation automatically created OAuth2 credentials. But what I need is service account credentials. Then I create one <code>Add credentials &gt; Service account</code> and download generated p12 file. I get the <code>clientId = 109160023321840004240</code> and <code>clientMail = account-1@project-id-uokwrcpiqeewhwvpdhb.iam.gserviceaccount.com</code> for this service account.</p>
  
  <p>I go back to my script and share it with the service account email with read&amp;write access <code>File &gt; Share</code>. First of all i get an email in my personal mailbox which notifies me that</p>
  
  <pre><code>Delivery to the following recipient failed permanently:
  
   account-1@project-id-uokwrcpiqeewhwvpdhb.iam.gserviceaccount.com
  </code></pre>
  
  <p>Then I publish the script as an execution API <code>Publish &gt; Publish as an execution API</code> with access to everybody.</p>
  
  <p>Now lets go on the PHP server side. Using the 'Google APIs Client Library for PHP' available here <a href="https://github.com/google/google-api-php-client" rel="nofollow">https://github.com/google/google-api-php-client</a> I try to call my script function from PHP:</p>
  
  <pre><code>$client = new Google_Client();
  $client-&gt;setClientId('109160023321840004240');
  $client-&gt;setApplicationName('myScript');
  
  $cred = new Google_Auth_AssertionCredentials(
      'account-1@project-id-okwrcpiqeewhwvpdhb.iam.gserviceaccount.com',
      [/*no scope nedeed for this simple script*/],
      file_get_contents('path_to_myScript.p12')
  );
  $client-&gt;setAssertionCredentials($cred);
  
  if ($client-&gt;getAuth()-&gt;isAccessTokenExpired()) {
      $client-&gt;getAuth()-&gt;refreshTokenWithAssertion($cred);
  }
  
  $service = new Google_Service_Script($client);
  $scriptId = 'MM8zQqofS1OIcnFiNSlm1CGEl5qMrMVBt';
  
  // Create an execution request object.
  $request = new Google_Service_Script_ExecutionRequest();
  $request-&gt;setFunction('get');
  $response = $service-&gt;scripts-&gt;run($scriptId, $request);
  </code></pre>
  
  <p>And here is the response I get all the time</p>
  
  <pre><code>Error calling POST https://script.googleapis.com/v1/scripts/MM8zQqofS1OIcnFiNSlm1CGEl5qMrMVBt:run: (403) The caller does not have permission
  </code></pre>
  
  <p>If, when I deploy the script, I choose to give access to 'Me only', i get the following response.</p>
  
  <pre><code>Error calling POST https://script.googleapis.com/v1/scripts/MM8zQqofS1OIcnFiNSlm1CGEl5qMrMVBt:run: (404) Requested entity was not found.
  </code></pre>
  
  <p>I would be so happy if one of you have an idea to help me :)</p>