1

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.

Mihail Russu
  • 2,526
  • 1
  • 17
  • 27
Maddy
  • 35
  • 2
  • 7
  • file with id docid should be public read – Zig Mandel Apr 13 '16 at 20:23
  • Its already public. I understood later that I have to provide permission under Developer Console Project. Go to 'Permission-Permissions for project "xxx" - Add Members' - and then add the domains or email IDs. Added email IDs/domains will be able to execute the script. However, I wasnt able too gmail domain. I have raised an [issue](https://code.google.com/p/google-apps-script-issues/issues/detail?id=5930&colspec=Stars%20Opened%20ID%20Type%20Status%20Summary%20Component%20Owner) with google support – Maddy Apr 14 '16 at 04:48

1 Answers1

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

Maddy
  • 35
  • 2
  • 7