URL存在时,Google Apps脚本UrlFetchApp返回404 [英] Google Apps Script UrlFetchApp returns 404 when the URL exists
问题描述
我正在尝试检索电子表格的pdf.每当我尝试时,都会出现以下错误:
I'm trying to retrieve a pdf of a spreadsheet. Whenever I try I get the following error:
Request failed for https://docs.google.com/a/firstcallres.com/spreadsheets/d/1ZPcW5cOQT5w28VUbr_JG9U-r7m6Uf-MDQcSmFOyhbE8/export?exportFormat=pdf&format=pdf&size=letter&portrait=false&fitw=true&source=labnol&sheetnames=false&printtitle=false&pagenumbers=false&gridlines=false&fzr=false&gid=1680655368 returned code 404.
如果登录时单击上面的URL,则会下载PDF.
If I click on the above URL when signed in, a PDF downloads.
如果我使用以下代码,我将获得一个登录页面作为响应. URL
是工作表网址,而url_ext
是参数.
If I use the following code, I will get a login page back as a response. The URL
is the sheet url, and the url_ext
are the parameters.
var response = UrlFetchApp.fetch(url + url_ext);
如果我随后将以下代码与OAuth令牌一起使用,则会收到404响应.
If I then use the following code with an OAuth token, I get a 404 response.
var response = UrlFetchApp.fetch(url + url_ext, {
headers: {'Authorization': 'Bearer ' + ScriptApp.getOAuthToken() }
});
为什么会这样?如何使用令牌通过应用脚本访问工作表以检索PDF?
Why is this happening? How can I use a token to access the sheet through apps script to retrieve a PDF?
它自发地开始处理我的测试应用程序脚本.但是,如果我在另一个应用程序脚本中创建完全相同的功能,它将再次失败并出现404错误.
It has spontaneously started working on my test apps script. However, if I create the exact same function in another apps script it fails with a 404 error again.
我做了一些更多的故障排除,然后将工作功能直接复制粘贴到了新的应用程序脚本项目中.该功能成功运行,此后不工作的功能现在可以工作了.
I did some more troubleshooting, and copy-pasted the working function right into the new apps script project. That function ran successfully, afterwards the non-working function now works.
在无法正常工作的功能失效与正常工作之间,未进行任何编辑.
There were no edits done to the non-working function between it failing, and working.
Edit2:我可以重现此错误::如果我使用第二个工作功能,然后复制-将其粘贴到新项目中,它将失败并显示404错误.我不知道为什么会这样,但是当它在完全相同的代码在其他地方工作时,我可以成功重现404错误.
I can reproduce this error: If I take my 2nd working function, and copy - paste it into a new project it will fail with the 404 error. I don't know WHY this happens, but I can successfully reproduce the 404 error when it works somewhere else with the exact same code.
推荐答案
您缺少发出此请求的适当范围.它应该返回未经身份验证的401而不是404.要解决此问题,您需要添加Drive范围.我通过在脚本中的某个位置进行简单的DriveApp.getRootFolder()
调用来完成此操作.这会将驱动器作用域添加到您的项目中.对DriveApp的任何调用都会添加作用域.
You are missing the proper scopes to make this request. It should return a 401 unauthenticated instead of a 404. To fix this you need to add the Drive scope. I do this by a simple DriveApp.getRootFolder()
call somewhere in your script. This will add the drive scope to your project. Any call to DriveApp will add the scope though.
这是一个示例脚本:
function exportToPDF() {
var url = "https://docs.google.com/spreadsheets/d/1oQ2zsbcCwuc_wdwgfFopRJn-5fGN3oY7L237ivxuxmM/export?exportFormat=pdf"; //add your options as you wish
var res = UrlFetchApp.fetch(url, {headers:{"Authorization":"Bearer " + ScriptApp.getOAuthToken()},muteHttpExceptions:true});
var newPdf = Utilities.newBlob(res.getContent(), "application/pdf", "Export.pdf")
DriveApp.createFile(newPdf);
}
这篇关于URL存在时,Google Apps脚本UrlFetchApp返回404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!