从另一个Google帐户执行Google Apps Script时出现403错误 [英] 403 error when executing Google Apps Script form a different google account

查看:69
本文介绍了从另一个Google帐户执行Google Apps Script时出现403错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


函数handleAuthResult(authResult){
if(authResult){
//访问令牌已被成功检索,请求可发送到API
} else {
//不能检索访问令牌,强制授权流程。
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 //可选。
};

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

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

以下是apps脚本。这使用Drive API和可执行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');

函数setData(参数){
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);

$ / code>

当我授权使用拥有apps脚本的gmail ID和项目(我自己的Gmail帐户)。但是,当我使用不同的gmail帐户进行身份验证时,我得到以下错误:

 错误:{code:403,message:The调用者没有权限,status:PERMISSION_DENIED} 
code:403
message:调用者没有权限
status:PERMISSION_DENIED

我打算公开这个应用程序,任何人都应该能够使用他们的Gmail帐户进行身份验证并执行脚本。我怎么做?请帮助。

解决方案

伙计们,我后来发现了这个问题。它恰好是在开发者控制台中的权限问题。我们必须在Developer Console Project下为应用程序脚本关联的项目分配权限。因此,请按照以下步骤操作:
$ b


  • 打开您的应用程序脚本转至资源 - 开发人员控制台项目

  • 单击项目名称显示在蓝色下,此脚本是目前与项目关联的
    :它会将您重定向到Developer
    Console Project。

  • 单击点击左上角的菜单,然后点击
    权限

  • 在权限点击添加成员

  • 在成员类型中,您想要提供
    权限和所需权限级别的电子邮件ID或域名。点击'添加'



您完成了。 然而,我不能够添加整个gmail域,也不能添加allAuthenticatedUsers。我提出了 issue with google support


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.

解决方案

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

这篇关于从另一个Google帐户执行Google Apps Script时出现403错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆