Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.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
从文件URL查找应用程序脚本发布的URL-按发布的Web应用程序URL搜索文件_Url_Google Apps Script_Google Apps_Google Sites - Fatal编程技术网

从文件URL查找应用程序脚本发布的URL-按发布的Web应用程序URL搜索文件

从文件URL查找应用程序脚本发布的URL-按发布的Web应用程序URL搜索文件,url,google-apps-script,google-apps,google-sites,Url,Google Apps Script,Google Apps,Google Sites,我们从客户以前的开发人员那里继承了不少谷歌应用脚本项目。应用程序脚本通过嵌入式小部件部署在谷歌网站(sites.Google.com)的各个页面上。每当我们需要处理其中一个项目时,我们都能够通过以下方式找到该项目: 转到sites.google.com上包含小部件的页面 编辑小部件 注意到公布的网址 进入script.google.com 打开/编辑名为“喜欢”的项目 单击发布>部署为web应用 将“当前web应用程序URL”与上面步骤3中的内容进行比较 这是一个相当乏味的过程,但到目前为止,它

我们从客户以前的开发人员那里继承了不少谷歌应用脚本项目。应用程序脚本通过嵌入式小部件部署在谷歌网站(sites.Google.com)的各个页面上。每当我们需要处理其中一个项目时,我们都能够通过以下方式找到该项目:

  • 转到sites.google.com上包含小部件的页面
  • 编辑小部件
  • 注意到公布的网址
  • 进入script.google.com
  • 打开/编辑名为“喜欢”的项目
  • 单击发布>部署为web应用
  • 将“当前web应用程序URL”与上面步骤3中的内容进行比较
  • 这是一个相当乏味的过程,但到目前为止,它一直在发挥作用

    其中一个小工具在通过sites.google.com访问时开始显示“需要授权…”消息,因此我们需要跟踪它所属的项目。我们完成了上面的步骤1-3,但找不到任何URL与该小工具匹配的项目

    我的直觉是,组织内部的其他人(不是开发人员的帐户)拥有该项目,但可能有5或6个不同的人,他们中没有一个是开发人员,也没有一个是特别有技术头脑的人。另一种可能是开发人员帐户确实拥有该项目,但它的名称不好,我不太愿意多次通过步骤5-7找到它


    有没有一种方法可以根据特定项目的URL定位该项目?script.google.com上的搜索工具似乎只搜索项目名称,不幸的是,这在这种情况下没有帮助。

    您可以使用应用程序脚本API获取脚本的“部署ID”

    首先,您需要使用
    DriveApp
    获取应用程序脚本项目文件的列表。然后,您需要遍历所有文件,获取文件ID,并使用文件ID获取部署信息

    每个项目都有一个部署列表。首先获取部署,然后从JSON对象获取部署Id

    要以我概述的方式使用Apps脚本API,必须在appsscript.json清单文件中设置所需的作用域

    以下是设置外观的示例:

    {
      "timeZone": "America/New_York",
      "dependencies": {
      },
      "webapp": {
        "access": "ANYONE",
        "executeAs": "USER_ACCESSING"
      },
      "exceptionLogging": "STACKDRIVER",
      "oauthScopes": ["https://www.googleapis.com/auth/script.projects", 
                      "https://www.googleapis.com/auth/drive.scripts", 
                      "https://www.googleapis.com/auth/drive", 
                      "https://www.googleapis.com/auth/script.container.ui", 
                      "https://www.googleapis.com/auth/script.external_request", 
                      "https://www.googleapis.com/auth/script.scriptapp",
                      "https://www.googleapis.com/auth/script.deployments",
                      "https://www.googleapis.com/auth/script.deployments.readonly"]
    }
    
    当您第一次运行代码时,您将得到授权权限的提示。但是,即使您授权了权限,您仍然需要转到开发人员控制台并为项目启用应用程序脚本API

    因此,您将从一个项目运行代码,以获取所有应用程序脚本文件的列表,然后获取每个项目的部署,并从部署中获取部署ID

    第一次运行代码时,请查看错误消息。例如,查看日志。您将在日志中看到如下错误消息:

    [18-06-22 08:51:32:841 EDT] response: {
      "error": {
        "code": 403,
        "message": "Apps Script API has not been used in project abc123 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/script.googleapis.com/overview?project=abc123 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.",
        "status": "PERMISSION_DENIED",
        "details": [
          {
            "@type": "type.googleapis.com/google.rpc.Help",
            "links": [
              {
                "description": "Google developers console API activation",
                "url": "https://console.developers.google.com/apis/api/script.googleapis.com/overview?project=abc123"
              }
            ]
          }
        ]
      }
    }
    
    将Url复制到开发者仪表板,然后将其粘贴到浏览器地址栏中。在仪表板中,启用API

    以下是您需要使用的代码示例:

    function searchForProjectWithCertainID() {
      var files,params,projectID_toFind,rtrn,thisFileID;
      
      projectID_toFind = "Put ID to find here";
      
      //params = 'mimeType contains "json"';
      //files = DriveApp.searchFiles(params);
      
      files = DriveApp.getFilesByType(MimeType.GOOGLE_APPS_SCRIPT);//Get all Apps Script files
      
      while (files.hasNext()) {
        thisFileID = files.next().getId();
        
        //Logger.log(thisFileID)
        
        rtrn = getDeploymentID(thisFileID);
        
        if (rtrn === projectID_toFind) {
          break;
        }
      }
    }
    
    function getDeploymentID(scriptId) {
    
      var errMsg,L,options,response,theAccessTkn,url;
    
      theAccessTkn = ScriptApp.getOAuthToken();
      
      url = "https://script.googleapis.com/v1/projects/" + scriptId + "/deployments";
    
      options = {
        "method" : "GET",
        "muteHttpExceptions": true,
        "headers": {
          'Authorization': 'Bearer ' +  theAccessTkn
        }
      };
    
      response = UrlFetchApp.fetch(url,options);
      Logger.log('response: ' + response)
    
      response = JSON.parse(response);//The response must be parsed into JSON even though it is an object
      
      L = response.deployments.length;
      //Logger.log('response.deployments.length: ' + response.deployments.length)
      
      if (typeof response === 'object') {
        errMsg = response.error;
        if (errMsg) {
          errMsg = errMsg.message;
          return 'err' + errMsg;
        }
      }
      
      //Logger.log(response.deployments[L - 1].deploymentId);
      
      return response.deployments[L - 1].deploymentId;
    }
    

    关键词:应用程序脚本、项目ID、部署ID、应用程序脚本API、已发布URL