Google script replaceAllShapesWithImage with image from drive不再适用

Den*_*ier 2 google-apps-script google-drive-api google-slides google-slides-api

从昨天开始,我的谷歌脚本之一不再起作用。剧本

  1. 在驱动器上拍照
  2. 复制幻灯片
  3. 用图像替换形状

但我收到了这个错误:

“提供的图片格式不受支持。”

-> 我授予对图像的所有访问权限:它不会改变任何内容

-> 如果我在驱动器外使用一个 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);


}
Run Code Online (Sandbox Code Playgroud)

Tan*_*ike 10

这个答案怎么样?请将此视为几种可能的答案之一。

问题和解决方法:

我认为您的问题是由于以下官方文件的修改造成的。

首先,我们正在更改 Google Drive API 的授权。如果您使用查询参数中的访问令牌授权对 Drive API 的下载请求,您将需要迁移您的请求以使用 HTTP 标头进行身份验证。从 2020 年 1 月 1 日开始,将不再支持对使用查询参数中的访问令牌进行身份验证的 files.get、revisions.get 和 files.export 端点的下载调用,这意味着您需要更新身份验证方法。

根据上述情况,var imageUrl = file.getDownloadUrl() + "&access_token=" + ScriptApp.getOAuthToken();无法使用的 URL 。例如,当它访问 URL 时,即使使用访问令牌也会显示登录屏幕。

为了避免这个问题,下面的修改怎么样?

改装要点:

  • 该文件公开共享并放入 Google 幻灯片。然后,共享文件被关闭。
    • 在这种情况下,即使关闭文件共享,也不会删除幻灯片上的放置图像。
  • webContentLink 用作 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
}
Run Code Online (Sandbox Code Playgroud)

参考:

如果我误解了您的问题并且这不是您想要的方向,我深表歉意。