UrlFetch 从电子表格 URL 中获取 404 错误 [英] UrlFetch getting 404 error from spreadsheet URL

查看:23
本文介绍了UrlFetch 从电子表格 URL 中获取 404 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 UrlFetchApp 将电子表格导出为 PDF 时,我一直收到 404 - 未找到请求的实体 响应.如果我将相同的 URL 放入浏览器,则 PDF 下载会正常启动.

I am consistently getting a 404 - Requested entity not found response when I use UrlFetchApp to export a spreadsheet as a PDF. If I put the same URL into a browser, the PDF download initiates properly.

我该怎么做才能让这个示例脚本工作?

What can I do to get this example script to work?

以下是一个简单的共享(只读)电子表格的 PDF 导出 URL 示例:https://docs.google.com/spreadsheets/d/133KPZyxAATG3AAY9iCSFWabefyFvQnENkHh0dyqh7MI/export?exportFormat=pdf&format=pdf&gid=0&size=letter&portrait=true&fitw=titles=true&lines&falseprintnames=false&fzr=false

Here's an example PDF export URL, for a simple shared (read-only) spreadsheet: https://docs.google.com/spreadsheets/d/133KPZyxAATG3AAY9iCSFWabefyFvQnENkHh0dyqh7MI/export?exportFormat=pdf&format=pdf&gid=0&size=letter&portrait=true&fitw=true&sheetnames=false&printtitle=false&pagenumbers=false&gridlines=false&fzr=false

如果您在浏览器中单击该 URL,它会获取一个 PDF Blob,这通常会启动浏览器的文件下载对话框.(这是一个公共工作表,但在生产中该工作表是私有的.)

If you click on that URL in your browser, it GETs a PDF Blob, which typically launches the browser's file download dialog. (This is a public sheet, but in production the sheet is private.)

function notFunctioning() {
  var options = {
    headers: {
      Authorization:"Bearer "+ScriptApp.getOAuthToken()
    },
    muteHttpExceptions : true        /// Get failure results
  }

  url = "https://docs.google.com/spreadsheets/d/133KPZyxAATG3AAY9iCSFWabefyFvQnENkHh0dyqh7MI/export?exportFormat=pdf&format=pdf&gid=0&size=letter&portrait=true&fitw=true&sheetnames=false&printtitle=false&pagenumbers=false&gridlines=false&fzr=false";
  var response = UrlFetchApp.fetch(url, options);
  
  var status = response.getResponseCode();
  var result = response.getContentText();  
  debugger; // status:404  8'(
}

结果字符串的内容为:

抱歉,您请求的文件不存在.

Sorry, the file you have requested does not exist.

确保您拥有正确的 URL,并且文件的所有者没有删除它.

Make sure that you have the correct URL and that the owner of the file hasn't deleted it.

当然,文件确实存在,等等.我假设我遗漏了一些身份验证或授权步骤.请注意,此代码与 将所有工作表转换为PDF 和 Google Apps 脚本,它曾经工作过...

Of course, the file does exist, etc. I'm assuming that there is some authentication or authorization step that I've missed. Note that this code is essentially the same as in Convert all sheets to PDF with Google Apps Script, which worked once upon a time...

  • 有报告 问题 5417 与执行 API 相关,但由于它还涉及文件传输和 Google 的 URL,我认为其中包含的建议可能适用.该建议(并非来自谷歌,请记住)是确保脚本与您用于身份验证的同一个谷歌开发者控制台项目相关联".我创建了一个新的开发控制台项目,并更改了 Apps Script 应用程序以使用它.虽然这触发了新的授权周期,但最终并没有解决 404 - 未找到请求的实体.

  • There is a reported Issue 5417 that pertains to the Execution API, but as it also involved file transfers and Google's URLs, I thought the advice it contained might apply. That advice (not from Google, mind) was to ensure the "script is associated with the same Google Developer console project that you used for authentication." I created a new dev console project, and changed the Apps Script application to use it. While that triggered a new authorization cycle, it did not ultimately fix the 404 - Requested entity not found.

A Google 文档编辑器URL 格式更改 是在一年前宣布的.我使用的是消费者帐户,而不是域帐户,因此这似乎不适用.此外,该脚本从那时起就起作用了.(不过,我想知道它是否确实影响事情,以及浏览器是否被重定向而 GAS 服务器没有.)

A Google Docs editors URL format change was announced a year ago. I'm using a consumer account, not a domain account, so it seems that this should not apply. Further, the script has worked since that time. (Still, I wonder if it does affect things, and whether the browser is being redirected while the GAS servers are not.)

推荐答案

虽然此技术不直接使用高级驱动器服务,但需要那些权限用于通过 URL 访问您的 Google Drive 文件的脚本.

While this technique does not directly use the Advanced Drive Service, it is those permissions that are needed for the script to access your Google Drive files via their URLs.

A 注意问题 3579:将电子表格转换为 PDF 失败,新的Sheets 提供了提示:

A note on Issue 3579: Converting spreadsheet as PDF fails with new Sheets provides the hint:

请记住,该解决方案仅在 Drive 或 DriveApp 出现在脚本中的某处时才有效,以便 Drive OAuth 范围被请求并可以传递.检查以确保所有脚本都如此.

Keep in mind that solution only works if Drive or DriveApp appears somewhere in the script, so that the Drive OAuth scopes are requested and can be passed along. Check to ensure this is true for all scripts.

添加一个包含 DriveApp 函数调用的虚拟函数就足够了:

It's sufficient to add a dummy function containing a DriveApp function call:

/**
 * Dummy function for API authorization only.
 */
function forAuth_() {
  DriveApp.getFileById("Just for authorization"); // https://code.google.com/p/google-apps-script-issues/issues/detail?id=3579#c36
}

然后通过资源 > 高级驱动器服务"启用高级驱动器服务...",以及开发者控制台.(有关详细信息,请参阅.)

Then enable the Advanced Drive Service through "Resources > Advanced Drive Services...", and the developer console. (See this for more info.)

就我而言,我的脚本一直在运行,然后停止了.我相信,当我最初编写脚本时,我曾尝试使用 Advanced Drive Service 导出链接 (这不起作用,顺便说一句),所以我一直在启用该服务.但最近,由于其他更改,我不得不重新授权脚本,我认为同时取消了对云端硬盘的授权.

In my case, I had a script that had been working, and then stopped. I believe that when I was originally writing the script, I'd attempted to use the Advanced Drive Service export links (which don't work, btw), so I'd been through enabling the service. But recently, I'd had to reauthorized the script due to other changes, and I think that removed the authorization for Drive at the same time.

这篇关于UrlFetch 从电子表格 URL 中获取 404 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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