Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other

 

 

I've encountered an issue with the listGroups function returning an error:
API call to directory.groups.list failed with error: Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.

The error appears whenever there is a call to SpreadsheetApp.getActiveSpreadsheet() in the test function, even if the line is commented out. If I remove the SpreadsheetApp.getActiveSpreadsheet() line completely, listGroups works without any authentication issues.

Strangely, SpreadsheetApp.getActiveSpreadsheet() works fine on its own outside of listGroups, without throwing any errors.

Here's the code snippet for reference:

 

 

 

function listGroups() {
  try {
    const groups = AdminDirectory.Groups.list({
      domain: 'playrix.com',
      auth: ScriptApp.getOAuthToken()
    });

    if (groups.groups && groups.groups.length > 0) {
      Logger.log('Groups in domain playrix.com:');
      groups.groups.forEach(group => {
        Logger.log(`Group: ${group.name} (ID: ${group.id})`);
      });
    } else {
      Logger.log('No Groups in domain!');
    }
  } catch (error) {
    Logger.log(`Error: ${error.message}`);
  }
}

function test() {
  //var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
}

 

 

 

Has anyone experienced a similar issue, or does anyone know why SpreadsheetApp.getActiveSpreadsheet() might be interfering with authentication in this way?

0 0 76
0 REPLIES 0