title: Gmail API Signature Server Side
body: <p>I want to change all <code>signatures</code> from my Gmail domain. This domain has many accounts, and I need to change it from <code>server-side</code>.</p>
  
  <p>I'm using <code>php</code>, and I started my project with:
  php composer.phar require <code>google/apiclient:2.0</code></p>
  
  <p>I wrote one code, but when I try to update one email (like teste@mydomain.com), I receive:</p>
  
  <pre><code>{ "error": { "errors": [ { "domain": "global", "reason": "insufficientPermissions", "message": "Insufficient Permission" } ], "code": 403, "message": "Insufficient Permission" } }
  </code></pre>
  
  <p>My code <code>(using API client library)</code> is something like:</p>
  
  <pre><code>&lt;?php
  
  // initialize gmail
  function getService() {
      try {
          include_once __DIR__ . '/vendor/autoload.php';
  
          $client = new Google_Client();
  
          $credentials_file = __DIR__ . '/credentials/gmailAPI.json';
  
          // set the location manually. Credential server-side
          $client-&gt;setAuthConfig($credentials_file);
  
          $client-&gt;setApplicationName("GmailAPI");
          $client-&gt;setScopes(['https://apps-apis.google.com/a/feeds/emailsettings/2.0/']);
  
          $gmail = new Google_Service_Gmail($client);
  
          return $gmail;
      } catch (Exception $e) {
          throw new Exception($e-&gt;getMessage());
      }
  }
  
  function updateSignature(&amp;$gmail) {
      try {
          // Start sendAs
          $signature = new Google_Service_Gmail_SendAs();
          // Configure Signature
          $signature-&gt;setSignature("Any HTML text here.");
  
          // Update account and print answer
          var_dump($gmail-&gt;users_settings_sendAs-&gt;update("someEmail@myDomain.com.br","someEmail@myDomain.com.br",$signature));
      } catch (Exception $e) {
          throw new Exception($e-&gt;getMessage());
      }
  
  }
  
  try {
      $gmail = getService();
      updateSignature($gmail);
  } catch (Exception $e) {
      echo $e-&gt;getMessage();
  }
  
  ?&gt;
  </code></pre>
  
  <p>My credential file (gmailAPI.json) is one service account key, and I'm using Google for Work.</p>
  
  <p>I created this credential using one administrator account from this domain.</p>
  
  <p><code>My credential file is:</code></p>
  
  <pre><code>{
    "type": "service_account",
    "project_id": "myProjectId",
    "private_key_id": "myPrivateKeyid",
    "private_key": "myPrivateKey",
    "client_email": "gmailapi@projectid.iam.gserviceaccount.com",
    "client_id": "myId",
    "auth_uri": "url",
    "token_uri": "url",
    "auth_provider_x509_cert_url": "url",
    "client_x509_cert_url": "url"
  }
  </code></pre>
  
  <p><strong>Edit 1</strong></p>
  
  <p>I changed the scopes as instructed, and now my scopes are:</p>
  
  <p>$client->setScopes(['<a href="https://www.googleapis.com/auth/gmail.settings.basic" rel="nofollow noreferrer">https://www.googleapis.com/auth/gmail.settings.basic</a>','<a href="https://www.googleapis.com/auth/gmail.settings.sharing" rel="nofollow noreferrer">https://www.googleapis.com/auth/gmail.settings.sharing</a>']);</p>
  
  <p>I also added permision on Google (/AdminHome?chromeless=1#OGX:ManageOauthClients) to my service account key.</p>
  
  <p>I tried API explorer and it works. When i changed the scopes, the error changed to:</p>
  
  <p>{ "error": { "errors": [ { "domain": "global", "reason": "failedPrecondition", "message": "Bad Request" } ], "code": 400, "message": "Bad Request" } }</p>
  
  <p>I'm using this command:</p>
  
  <p>var_dump($gmail->users_settings_sendAs->update("someEmail@myDomain.com.br","someEmail@myDomain.com.br",$signature));</p>
  
  <p>I tried also</p>
  
  <p>var_dump($gmail->users_settings_sendAs->get("someEmail@myDomain.com.br","someEmail@myDomain.com.br"));</p>
  
  <p>But I received same error.</p>