title: "PERMISSION_DENIED: Missing or insufficient permissions" when firestore function called using google cloud task
body: <p>I have created one function which adds an entry to the Firestore database.</p>
  <pre><code>const functions = require(&quot;firebase-functions&quot;);
  
  const admin = require('firebase-admin');
  admin.initializeApp();
  
  exports.addMessage = functions.https.onRequest(async (req, res) =&gt; {
      const original = 'dhara';
      const writeResult = await admin.firestore().collection('messages').add({original: original});
      res.json({result: `Message with ID: ${writeResult.id} added.`});
  });
  </code></pre>
  <p>Created queue by following command</p>
  <pre><code>gcloud tasks queues create auction-autobid-queue
  </code></pre>
  <p>Then created Google Cloud Task.</p>
  <pre><code>const serviceAccount = require('./my-service-account.json');
  
  const {CloudTasksClient} = require('@google-cloud/tasks');
  
  const client = new CloudTasksClient({
    credentials: serviceAccount
  });
  
  async function createTask() {
       const project = 'my-project';
       const queue = 'auction-autobid-queue';
       const location = 'europe-west1';
       const payload = 'Hello, World!';
       const serviceAccountEmail = &quot;my-name@my-project.iam.gserviceaccount.com&quot;;
        const parent = client.queuePath(project, location, queue);
    
      const task = {
        httpRequest: {
          httpMethod: 'GET',
          url: 'https://us-central1-my-project.cloudfunctions.net/addMessage',
          oidcToken: {
            serviceAccountEmail,
          },
        },
      };
    
      const inSeconds = 60;
      if (inSeconds) {
       task.scheduleTime = {
          seconds: inSeconds + Date.now() / 1000
        };
      }
    
      console.log('Sending task:');
      console.log(task);
      const request = {parent, task};
      const [response] = await client.createTask(request);
      const name = response.name;
      console.log(`Created task ${name}`);
    }
  
    process.on('unhandledRejection', err =&gt; {
      console.error(err.message);
      process.exitCode = 1;
    });
  
    createTask();
  </code></pre>
  <p>I added this task to an queue. Now when I run this task from the Google Cloud Console, it throws error.  I checked in firebase functions log and I can see function called but it throws me</p>
  <blockquote>
  <p>Error: 7 PERMISSION_DENIED: Missing or insufficient permissions.</p>
  </blockquote>
  <p><a href="https://i.stack.imgur.com/sjhJc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sjhJc.png" alt="enter image description here" /></a></p>
  <p>I have a service account which has
  <strong>Cloud Functions Developer</strong>
  <strong>Cloud Functions Service Agent</strong>
  <strong>Owner</strong> Roles.</p>
  <p>I can't get what is the issue here as assigned roles are pretty good.</p>