403 error when executing Google Apps Script form a

2020-08-01 07:43发布

问题:

I have this javascript to extract html table and then pass the arrays to google apps script as parameters.

var CLIENT_ID = 'some ID';
var SCRIPT_ID = 'some ID';
var SCOPES = ['https://www.googleapis.com/auth/drive.file'];

    function handleAuthClick(event) {
            gapi.auth.authorize(
                {'client_id': CLIENT_ID, 'scope': SCOPES, 'immediate': true},
                handleAuthResult);
    }

           function handleAuthResult(authResult) {
            if (authResult) {
              // Access token has been successfully retrieved, requests can be sent to the API
            } else {
              // No access token could be retrieved, force the authorization flow.
              gapi.auth.authorize(
                  {'client_id': CLIENT_ID, 'scope': SCOPES, 'immediate': false},
                  handleAuthResult);
            }
          }


    function exportGsheet() {
        var myTableArray = [];
        $("table#fin tr").each(function() {
        var arrayOfThisRow = [];
        var tableData = $(this).find('td');
        if (tableData.length > 0) {
        tableData.each(function() { arrayOfThisRow.push($(this).text()); });
        myTableArray.push(arrayOfThisRow);
        }
        });

        var params = JSON.stringify(myTableArray);
        var request = {
            'function': 'setData',
            'parameters': params,
            'devMode': true // Optional.
        };

        var op = gapi.client.request({
            'root': 'https://script.googleapis.com',
            'path': 'v1/scripts/' + SCRIPT_ID + ':run',
            'method': 'POST',
            'body': request
        });

        op.execute(function(resp){opensheet(resp)});
        }

Below is the apps script. This uses Drive API and Executable API.

var DOC_ID = 'some id';
var formattedDate = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd'T'HH:mm:ss'Z'"); 
var folder = DriveApp.getFolderById('some id');

function setData(parameters) {
  var getFile = DriveApp.getFileById(DOC_ID);
  var file = getFile.makeCopy(formattedDate, folder);
  var ss = SpreadsheetApp.open(file);
  var ssId = ss.getId();
  ss.getSheets()[0].getRange(5,1,50,24).clear();
  var e = JSON.parse(parameters);
  var outerArray = [];
  for(var i = 0; i < e.length; i++) {
    outerArray.push(e[i]);
  }
  ss.getSheets()[0].getRange(5, 2, outerArray.length, outerArray[0].length).setValues(outerArray);
  return {"url":ssId};
  Logger.log(ssId);
}

Everything works fine when I authorize using the gmail ID that owns the apps script and project (my own gmail account). But when I authenticate using a different gmail account I get the below error:

error: {code: 403, message: "The caller does not have permission", status: "PERMISSION_DENIED"}
code: 403
message: "The caller does not have permission"
status: "PERMISSION_DENIED"

I intend to make this application public and anyone should be able to authenticate using their gmail account and execute the script. How do I do that? Please help.

回答1:

Folks, I figured out the problem later. It happened to be permission issue n Developer console. We have to assign permission under Developer Console Project for the project which the apps-script is associated. So follow these steps:

  • Open your apps script Go to Resources-Developers Console Project
  • Click on the project name appearing in blue under "This script is
    currently associated with project:" It will redirect you to Developer Console Project.
  • Click on Menu on the left hand side upper corner and click on Permissions
  • Under Permissions click on Add members
  • In the member type the email ID or domain you want to provide permission and desired permission level. Click on 'Add'

You are done.

However, I wasnt able to add the entire gmail domain, nor able to add allAuthenticatedUsers. I have raised an issue with google support