Search code examples
google-apps-scriptgoogle-sheetsgoogle-drive-apicustom-function

GoogleJsonResponseException: API call with error: Login Required


I made a script using the advanced service API Google Drive. It's working fine when we stay into the app script GUI :

/** @OnlyCurrentDoc */
function GetListOfDrivesName() {
  const ALL_AVALIBLE_DRIVE = Drive.Drives.list();
  return ALL_AVALIBLE_DRIVE.items.map(driveData => driveData = driveData.name)
}

However, when it's called from cell into Google Sheet we got an error

enter image description here

Error message is like below : GoogleJsonResponseException: API call to drive.drives.list failed with error: Login Required.

I guess it's some authentification & authorisation to ask before using the sheet.

Unfortuantly I have no idea how to request that ! I read doc but it's sound like it will be asked promptly. In addition, trigger have been added for this function but it neither worked

Any idea ? Thanks in advance !


Solution

  • Hi @Yuri Khristich you were right, using ui is a good work around , code turn up to work corectly

    function onOpen() {
      var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
      var ui = SpreadsheetApp.getUi();
      ui.createMenu('[FM Script for G Drive]') 
      .addItem('test','showAvaillableDrive')
      .addToUi(); 
    }
    function showAvaillableDrive(){
       var ui = SpreadsheetApp.getUi(); 
        var resultat = ui.prompt(GetListOfDrivesName())
    }
    function GetListOfDrivesName() {
      const ALL_AVALIBLE_DRIVE = Drive.Drives.list();
      return ALL_AVALIBLE_DRIVE.items.map(driveData => driveData = driveData.name)
    }