GAS(谷歌应用程序脚本)/谷歌驱动器API:从预重定向链接检索重定向的URL

时间:2015-09-02 16:50:35

标签: javascript redirect google-apps-script

首先,先感谢您的回复/解决方案。

免责声明:此解决方案不用于托管图像,而是内部流程,由于100个原因,不会遵循重定向来获取图像。

这个问题是: 如何使用Google App Script(或Drive API)从链接到google驱动器文件(在本例中为image.jpg)中收集最终重定向的URL,以便粘贴到Google工作表中。阅读此内容的任何人都可以认为该图片是公开共享的。

工作流: 该脚本从外部URL中提取图像文件:

var image = UrlFetchApp.fetch(imageUrl).getBlob();

该脚本然后重命名该图像,然后使用Drive API将其放入google驱动器文件夹:

name = String(name).toLowerCase().replace(/\s+/g, '').replace(/[^\w\s]/gi, ''); //removes all spaces, sets the name lower case and then removes all extra characters
      image.setName(name);


// This would convert the file to mime type image/jpeg but it doesn't work on some gif's or jpeg 200~c

      var contentType = image.getContentType();

     try{

        if (contentType != "image/jpeg"){
          image = image.getAs('image/jpeg');
        }

      } catch (err){
        Logger.log("Didn't work");
      }

  } 
    } else {
      return imageUrl;
      }

    if (contentType === "image/jpeg"){
        var file = {
          title: name +'.jpg',
          mimeType: 'image/jpg',
          "parents": [{
          "kind": "drive#fileLink",
          "id": "folderID" //imageFolderID
           }] 
         };



        file = Drive.Files.insert(file, image);
        var driveId = file.id ; 

然后,它将该文件的公共共享链接(字符串)粘贴到工作表中,以便以后通过其他程序进行检索。问题是下面代码生成的链接导致重定向。

所以真正的问题是:如何获取此链接被重定向到的最终URL? (提示:这不在下面的请求或响应中,所以我无法解析它们)

相关链接的示例:

预重定向:https://googledrive.com/host/0B5gf6NnVG8VQflQwZHBMZVJiNFhlbkpEWUY3OHZnWS02ZjBZa2NBQUVCWWljVWpVWm5zcTQ/abc1.jpg

发布重定向:https://050dfacf921e33f0950333e36cbaf7c8256bd042.googledrive.com/secure/AOjc2GPc7PRSJAfYLixwxgly6DSeHpDWD0XeuJDkW42pIe5YgjTBlwZ_MUeB-nKlYS-amWukCgcqtac4sKQs-D3ah4EFK7rPUHVwmNr0jh6EZ_1mwDfzgrToK1gUPxRUef_YKPZ4UWLYzMZYogQxuF-KwaDJsGgM5VUeQvyBDP2bfRKjCxoEMxrkewzi-Zwgp3dYgRxfWMxggLWP4Wmfadl3zJ4sD7erqU0n1yPFo8obxZt7MCx-V_PFJaG5XX0TFw6u3UehmB0oJNrSODRfbj8eh8ncNCGCkaGJ7etODSpb8fAXXULu9FAh9sc9likOEy3jezVil3xRpw0_dr3b_TXS_nBEwhFV1N7uwFONsSIRa0lThz2P4-XJnhiegtq95VCdrVDiQwFOBYgro6vEZSPefzz9PGQ-bkhwiNq7PA_7e2xCIdaMOIyoriphCdbW6FyUox405Tu60Pec9NB6pGUv78vi0Znrh5s8MJYhBInkP5CoN3obhc4qu5QrR3bro4ArtYm34A3vdg3rMhStFlatD8q4g7BOuL6iraOndgIPBHXQdwEtMqNwLhpLI59iefyRZzdNBTxm2Ly1ZInj4-EYKl4h5ajKoA==/host/0B5gf6NnVG8VQflQwZHBMZVJiNFhlbkpEWUY3OHZnWS02ZjBZa2NBQUVCWWljVWpVWm5zcTQ/abc1.jpg

我的第一次尝试:

var sharedUrl = 'https://googledrive.com/host/'+ folderId + "/" + driveName;
var response = UrlFetchApp.fetch(sharedUrl);
var request = UrlFetchApp.getRequest(cleanUrl);
Logger.log(response.getContentText()); //doesn't contain the final link
Logger.log(request); //neither do I
Logger.log(response); //nope

第二次尝试:我尝试使用传统的JS(.location)来解决这个问题,但这种做法并不奏效。 (是的,下面的代码在这一点上有意义,但我的多次迭代也不起作用:

//        Logger.log("Page location is " + cleanUrl.location.href);

第三次尝试:我找到了这个Stack Link How to detect a redirected URL?

但我无法正常工作:

try {
    HttpResponse response = client.execute(getRequest);
    final int statusCode = response.getStatusLine().getStatusCode();
    if(statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY){
        String location = response.getHeaders("Location")[0].toString();
        String redirecturl = location.replace("Location: ", "");
    }
}

任何鼓励,帮助或只是一般的戏弄都会受到赞赏,并感谢任何愿意投入两分钱的人!

1 个答案:

答案 0 :(得分:0)

默认情况下,Google Apps脚本跟随广告属实,因此您无法找到位置标头。所以你需要把它设置为假如下。

var url ='https://googledrive.com/host/' + file.id;
var options = {"method" : "GET","followRedirects" : false};
var redirecturl = UrlFetchApp.fetch(url, options).getAllHeaders().Location;