从昨天开始,我的Google脚本之一不再起作用。剧本
但是我得到了这个错误:
“提供的图像格式不受支持。”
->我拥有对图像的所有访问权限:它没有任何改变
->如果我在驱动器外添加一个URL,脚本将起作用
任何的想法
function test_image(){
var imageUrls = DriveApp.getFilesByName("DSC_3632.png");
var file = "undefined";
while ( imageUrls.hasNext()) {
var file = imageUrls.next();
}
var imageUrl = file.getDownloadUrl() + "&access_token=" + ScriptApp.getOAuthToken();
var model_file = DriveApp.getFileById("your-id");
var presentation = model_file.makeCopy("totot");
var presentation =Slides.Presentations.get(presentation.getId())
var requests = [{
"replaceAllShapesWithImage":
{
"imageUrl": imageUrl,
"imageReplaceMethod": "CENTER_INSIDE",
"containsText": {
"text": "toto",
"matchCase": false,
}
}
}];
var presentationId = presentation.presentationId
var createSlideResponse = Slides.Presentations.batchUpdate({
requests: requests
}, presentationId);
}
这个答案怎么样?请认为这只是几个可能的答案之一。
我认为您提出该问题的原因是由于对官方文件进行了以下修改。
首先,我们正在更改Google Drive API的授权。如果您使用查询参数中的访问令牌授权对Drive API的下载请求,则需要迁移请求以使用HTTP标头进行身份验证。从2020年1月1日开始,将不再支持对使用query参数中的访问令牌进行身份验证的files.get,versions.get和files.export端点的下载调用,这意味着您需要更新身份验证方法。
在上述情况下,var imageUrl = file.getDownloadUrl() + "&access_token=" + ScriptApp.getOAuthToken();
无法使用的URL 。例如,当访问URL时,即使使用访问令牌,也会显示登录屏幕。
为了避免此问题,如何进行以下修改?
https://drive.google.com/uc?export=download&id=###
。修改脚本后,它如下所示。
function test_image(){
var imageUrls = DriveApp.getFilesByName("DSC_3632.png");
var file; // Modified
while (imageUrls.hasNext()) {
file = imageUrls.next();
}
file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW); // Added
var imageUrl = "https://drive.google.com/uc?export=download&id=" + file.getId(); // Modified
var model_file = DriveApp.getFileById("your-id");
var presentation = model_file.makeCopy("totot");
var presentation =Slides.Presentations.get(presentation.getId())
var requests = [{
"replaceAllShapesWithImage": {
"imageUrl": imageUrl,
"imageReplaceMethod": "CENTER_INSIDE",
"containsText": {
"text": "toto",
"matchCase": false,
}
}
}];
var presentationId = presentation.presentationId
var createSlideResponse = Slides.Presentations.batchUpdate({requests: requests}, presentationId);
file.setSharing(DriveApp.Access.PRIVATE, DriveApp.Permission.NONE); // Added
}
如果我误解了您的问题,而这不是您想要的方向,我深表歉意。
好奇。为什么不发送
{'Authorization': 'Bearer '+ token}
。它说http标头有效吗?@TheMaster谢谢您的评论。在OP的脚本中,使用Slides API将文本替换为图像。为此,我修改了OP的脚本。在这种情况下,图像必须是URL。并且,要求URL能够从外部访问。因此,请求标头未用于Slides API的请求正文。这样,在我的修改中,文件被公开共享一次,并被映像替换,并且共享被关闭。当然,使用“幻灯片”服务时,可以直接使用Blob。如果我误解了您的评论,我深表歉意。
@TheMaster感谢您的答复。作为其他方法,当使用Google文档时,可以将文档中的图像作为时间URL检索,例如Google Photo。尽管这是临时的,但这些URL是公共的,因此也可以用于OP的情况。
我遇到了这个问题,尝试使用blob base64string,但是我收到另一个错误,抱怨url属性超出了字符串长度。终于解决了文件许可权的解决方法。