URL to upload to Google Drive

How about this workaround? Unfortunately, there are no URLs for directly uploading files without authorization on Google API. So I propose a workaround. I thought that this method may be easy for achieving what you want to do. Please think of this as one of several answers.

I think that by using Web Apps, you can achieve what you want to do. The flow of this workaround is as follows.

Flow :

1. Sample script

  • Create a standalone script.
    • Also you can do this flow at bound scripts. Here, a standalone script is used.
  • Copy and paste the following sample script, and save it.

Please run doGet(e). By this, although an error occurs, the authorization process can be done.

function doGet(e) {  var blob = UrlFetchApp.fetch(e.parameters.url).getBlob();  var id = DriveApp.createFile(blob).getId();  return ContentService.createTextOutput(id);}

2. Deploy Web Apps.

  • On script editor, at menu bar, select Publish -> Deploy as Web App.
  • At “Project version:”, input string.
  • At “Execute the app as:”, select “Me”.
  • At “Who has access to the app:”, select “Anyone, even anonymous”.
  • Click “Deploy” button.
  • Please copy “Current web app URL:”. The URL is https://script.google.com/macros/s/#####/exec.
    • You can upload files using this URL.
  • Click “OK”.

3. Upload a file from the URL

  • Query parameter is url=http://example.com/file.doc.
  • You can upload file.doc by accessing this URL https://script.google.com/macros/s/#####/exec?url=http://example.com/file.doc using your browser.
    • In this sample script, the upload file is created on root folder on your Google Drive.

Note :

  • About Web Apps, if you update your script, please redeploy Web Apps as new version. By this, the updated script is reflected.
  • This sample script is very simple. So please modify this to your environment. At that time, if you have any problems, feel free to tell me.
  • In this case, when users upload files using the Web Apps URL, the files are uploaded to Google Drive of the owner who deployed Web Apps. Because the script is run as the only owner.

If this was not useful for you, I’m sorry.

Edit :

I understand that you want to upload files from URL using php and javascript (I used jQuery here.). If my understanding is wrong, please tell me. doGet(e) of Google Apps Script is required to use these script. At first, please copy and paste doGet(e) and deploy Web Apps. For both script, the file ID of created file on Google Drive is returned as the response.

Upload by php

<?php    $url = 'https://script.google.com/macros/s/#####/exec?url=http://example.com/file.doc';    $curl = curl_init();    curl_setopt($curl, CURLOPT_URL, $url);    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);    curl_setopt($curl,CURLOPT_FOLLOWLOCATION, true);    $response = curl_exec($curl);    echo $response;    curl_close($curl);?>

Upload by jQuery

$.get("https://script.google.com/macros/s/#####/exec?url=http://example.com/file.doc", function(res) {    console.log(res);});

References :

Edit 2 :

When you want to make users upload files to own Google Drive, please deploy Web Apps by the following flow.

Flow :

1. Sample script

  • Create standalone script.
    • Also you can do this flow at bound scripts. Here, a standalone script is used.
  • Copy and paste following sample script, and save it.

Please run doGet(e). By this, although an error occurs, the authorization process can be done.

function doGet(e) {  var blob = UrlFetchApp.fetch(e.parameters.url).getBlob();  var id = DriveApp.createFile(blob).getId();  return ContentService.createTextOutput(id);}

2. Deploy Web Apps.

  • On script ediror, at menu bar, select Publish -> Deploy as Web App.
  • At “Project version:”, input string.
  • At “Execute the app as:”, select “User accessing the web app”.
  • At “Who has access to the app:”, select “Anyone”.
  • Click “Deploy” button.
  • Please copy “Current web app URL:”. The URL is https://script.google.com/macros/s/#####/exec.
    • You can upload files using this URL.
  • Click “OK”.

3. Upload a file from URL

  • Query parameter is url=http://example.com/file.doc.
  • You can upload file.doc by accessing this URL https://script.google.com/macros/s/#####/exec?url=http://example.com/file.doc using your browser.
    • In this sample script, the upload file is created on root folder on your Google Drive.
  • When users access the URL of https://script.google.com/macros/s/#####/exec?url=http://example.com/file.doc, the login screen to Google is displayed.
  • After logged in Google, the authorization screen is displayed.
  • By authorized it, file.doc is uploaded to user’s Google Drive. Because the script is run as each user.

Note :

  • Please deploy Web Apps by only one user. And please share the URL of Web Apps among all users.
  • In this case, users use the browser of each user, because the authorization process is required the browser.
    • If you use this as a HTML, you can put the URL of https://script.google.com/macros/s/#####/exec?url=http://example.com/file.doc as a link.
  • About Web Apps, if you update your script, please redeploy Web Apps as new version. By this, the updated script is reflected.

Edit 3:

In the case of use of Web Apps, when <div class="google-upload" data-url="http://example.com/file.doc"> is used, the sample script is as follows.

When users click Upload, file.doc is uploaded to user’s Drive. In order to use this sample script, please use the URL retrieved by deploying Web Apps using above “Edit 2”.

In this case, the login screen is displayed to each user. Users are required to authorize to use APIs after login by click the button on the screen. Is this what you want?

<html>  <head>    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.js"></script>  </head>  <body>    <div class="google-upload" data-url="http://example.com/file.doc">      <span style="background-color: #ddd">Upload</span>    </div>    <script>      $(function() {        $(".google-upload").click(function() {          var url = "https://script.google.com/macros/s/#####/exec"; // Please input the URL here.          var withQuery = url + "?url=";          window.open(withQuery + $('.google-upload').attr("data-url"), "_blank", "width=600,height=600,scrollbars=1");        });      });    </script>  </body></html>

Xổ số miền Bắc