I have written a Gmail add-on which, when the user opens an email, finds the label of the thread (the thread only has one label). If there are multiple threads with the same label, it fetches the messages in those threads. Otherwise it fetches the messages from the thread with the opened email.

When there is only one thread, it works fine. When there are multiple threads, I get an access token error: Access denied : Missing access token for authorization. Request: MailboxService.GetThread. [line: xx (the line highlighted in the code below) etc

The add-on uses the access token correctly as far as I can tell. The numbers of emails and threads are very small (three or four threads with a few messages in each).

Below is a simplified version of my code. Does anyone know why I should get the access token error when I try to access messages of other threads?

I have tried using other methods for building the array of messages (search, filter/function etc), but the result is the same: no problem with the single thread containing the trigger email, but access denied when accessing multiple threads.

function getAllMessagesWithTheSameLabel(message) {
  var threads = [];
  var messages = [];
  var thread = message.getThread();
  var label = thread.getLabels()[0];
  if (label.getThreads().length > 1) {
    threads = label.getThreads();
    **messages = GmailApp.getMessagesForThreads(threads);**
  } else {
    messages = GmailApp.getMessagesForThread(thread);
  }
}
0

There are 0 best solutions below