Skip to content

Instantly share code, notes, and snippets.

@projektorius96
Forked from skauk/DriveUpload.gs
Created November 27, 2021 15:18
Show Gist options
  • Save projektorius96/1d39bc622e2dfb7777cfcc5afd65e79b to your computer and use it in GitHub Desktop.
Save projektorius96/1d39bc622e2dfb7777cfcc5afd65e79b to your computer and use it in GitHub Desktop.

Revisions

  1. @skauk skauk revised this gist Feb 8, 2013. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions DriveUpload.gs
    Original file line number Diff line number Diff line change
    @@ -22,6 +22,18 @@ function uploadPdfOcr() {
    return DocsList.getFileById(uploadResponse.id);
    }

    function convert(fileId) {
    authorize();
    var key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // <-- developer key
    var params = {method:"post",
    oAuthServiceName: "drive",
    oAuthUseToken: "always"
    };
    var uploadRequest = UrlFetchApp.fetch("https://www.googleapis.com/drive/v2/files/"+fileId+"/copy?convert=true&key="+key, params);
    var uploadResponse = Utilities.jsonParse(uploadRequest.getContentText());
    return DocsList.getFileById(uploadResponse.id);
    }

    function authorize() {
    var oauthConfig = UrlFetchApp.addOAuthService("drive");
    var scope = "https://www.googleapis.com/auth/drive";
  2. @skauk skauk created this gist Dec 30, 2012.
    33 changes: 33 additions & 0 deletions DriveUpload.gs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    function uploadPdfOcr() {
    authorize();
    var key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // <-- developer key
    var file = UrlFetchApp.fetch("http://somewhere.com/path/file.pdf").getBlob();
    var metadata = { title: file.getName() }
    var params = {method:"post",
    oAuthServiceName: "drive",
    oAuthUseToken: "always",
    contentType: "application/pdf",
    contentLength: file.getBytes().length,
    payload: file.getBytes()
    };
    var uploadRequest = UrlFetchApp.fetch("https://www.googleapis.com/upload/drive/v2/files/?uploadType=media&ocr=true&key="+key, params);
    var uploadResponse = Utilities.jsonParse(uploadRequest.getContentText());
    var params = {method:"put",
    oAuthServiceName: "drive",
    oAuthUseToken: "always",
    contentType: "application/json",
    payload: Utilities.jsonStringify(metadata)
    };
    var metaRequest = UrlFetchApp.fetch("https://www.googleapis.com/drive/v2/files/"+uploadResponse.id+"?key="+key, params)
    return DocsList.getFileById(uploadResponse.id);
    }

    function authorize() {
    var oauthConfig = UrlFetchApp.addOAuthService("drive");
    var scope = "https://www.googleapis.com/auth/drive";
    oauthConfig.setConsumerKey("anonymous");
    oauthConfig.setConsumerSecret("anonymous");
    oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
    oauthConfig.setAuthorizationUrl("https://accounts.google.com/OAuthAuthorizeToken");
    oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
    }