title: Gmail Api problems
body: <p>We are currently trying to create a tool in Google Apps Script using OAuth2 for Authentication. The script is supposed to use the GMail api to carry out some tasks such as moving labels from one account to another.
  Documentation we have used says we need to create a service account with domain wide authority and create necessary components to allow it to work (i.e client_id and so on.. )</p>
  <p>We are getting the error below is anyone able to help?</p>
  <p>Code:</p>
  <pre><code>function getGmailService() {
  return OAuth2.createService('GmailMigration:' + user_email)
    // Set the endpoint URL.
    .setTokenUrl('https://oauth2.googleapis.com/token')
    .setAuthorizationBaseUrl(CREDENTIALS.auth_uri)
  
    // Set the private key and issuer.
    .setPrivateKey(CREDENTIALS.private_key)
    .setIssuer(CREDENTIALS.client_email)
  
    // Set the name of the user to impersonate. This will only work for
    // Google Apps for Work/EDU accounts whose admin has setup domain-wide
    // delegation:
    // https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority
    .setSubject(CLIENT_EMAIL)
  
    // Set the property store where authorized tokens should be persisted.
    .setPropertyStore(PropertiesService.getScriptProperties())
  
    // Set the scope. This must match one of the scopes configured during the
    // setup of domain-wide delegation.
    .setScope('https://mail.google.com https://www.googleapis.com/auth/gmail.modify 
  https://www.googleapis.com/auth/gmail.labels https://www.googleapis.com/auth/gmail.insert 
  https://www.googleapis.com/auth/gmail.addons.current.message.action https://www.googleapis.com/auth/gmail.readonly https://www.googleapis.com/auth/gmail.addons.current.message.metadata https://www.googleapis.com/auth/gmail.metadata');
  </code></pre>
  <p>}</p>
  <pre><code>function reset() {
  getGmailService().reset();
  </code></pre>
  <p>}</p>
  <pre><code>function run() {
  var service = getGmailService();
  if (service.hasAccess()) {
  console.log({Service: service})
  var url = 'https://gmail.googleapis.com/gmail/v1/users/{user_id}/labels/{message/label_id}?key='+CREDENTIALS.api_key;
  var response = UrlFetchApp.fetch(url, {
    headers: {
      Authorization: 'Bearer ' + service.getAccessToken()
    },
    muteHttpExceptions: true
  });
  var result = JSON.parse(response.getContentText());
  Logger.log(JSON.stringify(result, null, 2));} else {
  Logger.log(service.getLastError());
  </code></pre>
  <p>}
  }</p>
  <p>Error:</p>
  <p>{
  &quot;error&quot;: {
  &quot;code&quot;: 400,
  &quot;message&quot;: &quot;Precondition check failed.&quot;,
  &quot;errors&quot;: [
  {
  &quot;message&quot;: &quot;Precondition check failed.&quot;,
  &quot;domain&quot;: &quot;global&quot;,
  &quot;reason&quot;: &quot;failedPrecondition&quot;
  }
  ],
  &quot;status&quot;: &quot;FAILED_PRECONDITION&quot;
  }
  }</p>
  <p>Thanks</p>