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

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

问题描述

当我使用UrlFetchApp将电子表格导出为PDF格式时,我始终得到 404 - 未找到请求的实体响应。如果我将相同的URL放到浏览器中,PDF下载会正确启动。



我可以做些什么来获得这个脚本示例?

下面是一个简单的共享(只读)电子表格的PDF导出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



如果您在浏览器中点击该网址,则会获得一个PDF Blob,浏览器的文件下载对话框。 (这是一张公开表,但在生产中该表是私人的。)


$ b

function notFunctioning( ){
var options = {
headers:{
Authorization:Bearer+ ScriptApp.getOAuthToken()
},
muteHttpExceptions:true ///获取失败结果
}

url =https://docs.google.com/spreadsheets/d/133KPZyxAATG3AAY9iCSFWabefyFvQnENkHh0dyqh7MI/export?exportFormat=pdf&format=pdf&gid=0&size=letter& ;肖像=真安培; fitw =真安培; sheetnames =假安培; printtitle =假安培;页面编号=假安培;网格线=假安培; FZR =假;
var response = UrlFetchApp.fetch(url,options);

var status = response.getResponseCode();
var result = response.getContentText();
调试器; // status:404 8'(

结果字符串的内容是:


抱歉,您要求的档案不存在。

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


当然,文件 是否存在,等等。我假设有一些验证或授权步骤,我错过了。请注意,此代码基本上与将所有工作表转换为使用Google Apps脚本的PDF ,这些工作可以一次完成 / em>



调查:




  • 与执行有关的href =https://code.google.com/p/google-apps-script-issues/issues/detail?id=5417 =nofollow noreferrer> Issue 5417 API,但也是如此涉及文件传输和Google的网址,我认为它包含的建议可能适用。该建议(不是来自Google,建议)是确保脚本与您用于身份验证的Google Developer控制台项目相关联。我创建了一个新的开发控制台项目,并更改了Apps脚本应用程序以使用它。虽然这触发了新的授权周期,但它并未最终解决 404 - 未找到请求实体

  • A Google文档编辑器URL格式更改已宣布一年前。我使用的是消费者帐户,而不是域帐户,所以看起来这不适用。此外,该脚本自那时以来一直运作良好。 (仍然,我不知道它是否会影响事情,以及浏览器是否被重定向,而不是GAS服务器。)



解决方案

虽然此技术不直接使用高级云端硬盘服务,但它是那些权限这是脚本通过其URL访问您的Google云端硬盘文件所需的。



A note on 问题3579:将电子表格转换为PDF失败并显示新表格提示提示:



< blockquote>

请记住,该解决方案仅适用于Drive或DriveApp出现在脚本中的某处,以便Drive OAuth范围被请求并可以传递。检查以确保所有脚本均为真。


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


$ b / **
*仅用于API授权的虚拟函数。
* /
function forAuth_(){
DriveApp.getFileById(Just for authorization); // https://code.google.com/p/google-apps-script-issues/issues/detail?id=3579#c36
}

然后启用高级云端硬盘服务通过资源>高级云端硬盘服务...,以及开发者控制台。 (有关详情,请参阅。)






在我的情况中,我有一个脚本一直在工作,然后停下来。我相信,当我最初编写脚本时,我试图使用高级云端硬盘服务导出链接(哪些行不通,顺便说一句),所以我一直通过启用服务。但最近,由于其他更改,我不得不重新授权脚本,并且 同时移除了Drive的授权。


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?

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

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'(
}

The content of the result string is:

Sorry, the file you have requested does not exist.

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

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...

Investigations:

  • 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 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.)

解决方案

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 note on Issue 3579: Converting spreadsheet as PDF fails with new Sheets provides the hint:

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.

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.)


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天全站免登陆