Derek Morton
Derek Morton

Reputation: 1

Oauth2 service creation hasAccess() fails in Google Sheets script for all users except owner

I'm using the https://github.com/googleworkspace/apps-script-oauth2 library in a Google Sheet script to call an external API. It works well for the owner of the sheet. For all other users it fails in creating the service/property store? It fails the "Service.hasAccess()" condition.

I suspect I am missing some sort of permissions somewhere. I have given users Edit permissions on the Sheet and have gone through other various gyrations trying to figure this out. I decided to apply this script via a Standard Project.

Scope are applied explicitly in the manifest and all works swimmingly for the sheet owner.

''''''''Google Apps Script, Spreadsheet Script in GCP Standard Project

function authorizeUser() {

  var myService = getMyService();

  if (myService.hasAccess()) { 

  >FAILS THIS CONDITION for all except spreadsheet owner
  }

}

function getMyService() {

  return OAuth2.createService('sky')
  .setAuthorizationBaseUrl('https://oauth2../authorization')
  .setTokenUrl('https://oauth2.../token')
  .setClientId('fee......')
  .setClientSecret('Ighh.....')
  .setCallbackFunction('authCallback')
  .setPropertyStore(PropertiesService.getUserProperties())
  //.setScope('https://www.googleapis.com/auth/drive')
  // Below are Google-specific OAuth2 parameters.

  .setParam('login_hint', Session.getEffectiveUser().getEmail())
}

>I believe the failure is occurring in OAuth2.gs here

 function createService(serviceName) {
   return new Service_(serviceName);
}

OAuth2.gs: https://github.com/googleworkspace/apps-script-oauth2/tree/master/dist

Any thoughts?

D M

Upvotes: 0

Views: 371

Answers (1)

Derek Morton
Derek Morton

Reputation: 1

Apparently the suggested code to validate the service state in the apps-script-oauth2 library is not indicative of whether or not the Oauth process can be completed.

  1. Direct the user to the authorization URL Apps Script UI's are not allowed to redirect the user's window to a new URL, so you'll >need to present the authorization URL as a link for the user to click. The URL is >generated by the service, using the function getAuthorizationUrl().
function showSidebar() {
  var driveService = getDriveService();
  if (!driveService.hasAccess()) {

I was able to complete my Oauth process regardless of the state returned by has.Access() . I'm not sure if this is a time sensitive operation or something else is at play. At any rate I was able to proceed and develop a final solution as a GAS web app.

Upvotes: 0

Related Questions