Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 是否可以通过URL访问已部署的受保护Google Web应用,而无需每次从浏览器登录?_Google Apps Script_Web Applications_Oauth_Service Accounts_Google Workspace - Fatal编程技术网

Google apps script 是否可以通过URL访问已部署的受保护Google Web应用,而无需每次从浏览器登录?

Google apps script 是否可以通过URL访问已部署的受保护Google Web应用,而无需每次从浏览器登录?,google-apps-script,web-applications,oauth,service-accounts,google-workspace,Google Apps Script,Web Applications,Oauth,Service Accounts,Google Workspace,我部署了一个受保护的web应用程序,我希望在每次不登录的情况下触发它: 我想在不登录的情况下访问web应用程序URL: 根据此文档,如果不从浏览器登录,则无法执行以下操作: 如果Web应用程序的脚本使用某些作用域,客户端用户必须 通过自己的浏览器授权作用域 我假设scopes意味着web应用程序受到保护 我尝试过这个:但它要求“请求访问” 如果单击请求访问,则会显示以下内容: 在这一点上,我认为如果每次都不通过浏览器进行身份验证,就不可能设置一个具有作用域的服务帐户来触发受保护的已部署w

我部署了一个受保护的web应用程序,我希望在每次不登录的情况下触发它:

我想在不登录的情况下访问web应用程序URL:

根据此文档,如果不从浏览器登录,则无法执行以下操作:

如果Web应用程序的脚本使用某些作用域,客户端用户必须 通过自己的浏览器授权作用域

我假设
scopes
意味着web应用程序受到保护

我尝试过这个:但它要求“请求访问”

如果单击请求访问,则会显示以下内容:

在这一点上,我认为如果每次都不通过浏览器进行身份验证,就不可能设置一个具有作用域的服务帐户来触发受保护的已部署web应用程序。有人能证实这一点吗

我的假设是web应用程序的范围是
https://www.googleapis.com/auth/drive
因为它可以访问所有驱动器的文件

更新:(我尝试了但没有成功的内容)

我匹配了脚本中的范围:

到服务帐户:

上面的模糊区域是我从以下位置获取的客户端id:

我已使用以下脚本生成访问令牌:

function accessTokens(){
 var private_key = "-----BEGIN PRIVATE KEY-----*****\n-----END PRIVATE KEY-----\n"; // private_key of JSON file retrieved by creating Service Account
var client_email = "****@****.iam.gserviceaccount.com"; // client_email of JSON file retrieved by creating Service Account
var scopes = ["https://www.googleapis.com/auth/documents","https://www.googleapis.com/auth/forms","https://www.googleapis.com/auth/script.external_request","https://www.googleapis.com/auth/spreadsheets","https://www.googleapis.com/auth/userinfo.email"]; // Scopes


var url = "https://www.googleapis.com/oauth2/v3/token";
var header = {
  alg: "RS256",
  typ: "JWT",
};
var now = Math.floor(Date.now() / 1000);
var claim = {
  iss: client_email,
  scope: scopes.join(" "),
  aud: url,
  exp: (now + 3600).toString(),
  iat: now.toString(),
};
var signature = Utilities.base64Encode(JSON.stringify(header)) + "." + Utilities.base64Encode(JSON.stringify(claim));
var jwt = signature + "." + Utilities.base64Encode(Utilities.computeRsaSha256Signature(signature, private_key));

var params = {
  method: "post",
  payload: {
    assertion: jwt,
    grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer",
  },
};
var res = UrlFetchApp.fetch(url, params).getContentText();
Logger.log(res); 
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheetByName("Sheet1");
  sheet.getRange(1, 3).setValue(JSON.parse(res)['access_token']);
}

但仍然有相同的错误,它要求请求访问。

在这几天后,我找到了答案(当然是在帮助下)

  • 从部署的web应用程序脚本获取范围:
    文件>项目属性>范围
  • 添加范围以及
    https://www.googleapis.com/auth/drive
    在第
    页中管理API客户端访问
    (使用逗号分隔添加多个作用域:
    http…、http…,等等
  • 对于客户端名称,请从管理控制台的“服务帐户”页面获取客户端id:
  • 部署脚本
    Publish>deployaswebapp
  • 生成访问令牌(下面的说明)后,将访问令牌附加到部署的web应用程序url
    &access\u token=YOURTOKENHERE
  • 将此脚本与google工作表一起使用,它将在
    Sheet1
    的单元格
    A1
    中生成access_令牌(将4个变量替换为与您相关的信息):


    当你创建服务帐户时,你提供了吗?我昨天提供了,仍然没有运气:我认为这特别棘手,因为我有一个gsuite业务帐户。我有点困惑,如果你不想使用浏览器,为什么你需要通过Web App URL访问它,有什么原因不能使用常规应用程序脚本触发器在不访问web界面的情况下触发相关代码吗?@TheMaster找到了答案!需要对脚本进行一些调整,但遗漏了一些内容。谢谢你的链接。
    function accessTokens(){
     var private_key = "-----BEGIN PRIVATE KEY-----n-----END PRIVATE KEY-----\n"; // private_key of JSON file retrieved by creating Service Account
    var client_email = "*****@****.iam.gserviceaccount.com"; // client_email of JSON file retrieved by creating Service Account
    var scopes = ["https://www.googleapis.com/auth/documents","https://www.googleapis.com/auth/forms","https://www.googleapis.com/auth/script.external_request","https://www.googleapis.com/auth/spreadsheets","https://www.googleapis.com/auth/userinfo.email","https://www.googleapis.com/auth/drive"]; // Scopes
    var impersonate_email = "" //impersonate email
    
    var url = "https://www.googleapis.com/oauth2/v4/token";
    var header = {
      alg: "RS256",
      typ: "JWT",
    };
    var now = Math.floor(Date.now() / 1000);
    var claim = {
      iss: client_email,
      sub: impersonate_email,
      scope: scopes.join(" "),
      aud: url,
      exp: (now + 3600).toString(),
      iat: now.toString(),
    };
    var signature = Utilities.base64Encode(JSON.stringify(header)) + "." + Utilities.base64Encode(JSON.stringify(claim));
    var jwt = signature + "." + Utilities.base64Encode(Utilities.computeRsaSha256Signature(signature, private_key));
    
    var params = {
      method: "post",
      payload: {
        assertion: jwt,
        grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer",
      },
    };
    var res = UrlFetchApp.fetch(url, params).getContentText();
    Logger.log(res); 
        var ss = SpreadsheetApp.getActiveSpreadsheet();
        var sheet = ss.getSheetByName("Sheet1");
      sheet.getRange(1, 1).setValue(JSON.parse(res)['access_token']);
    }