2

I am trying to figure out how to call a google script via the api from php. Here is my script:

function doGet() {
  return ContentService.createTextOutput("hello world!");
}

I can call it as a web app via it's URL but what I am trying to accomplish will require it be called via the API. Google's documentation is so confusing it's almost useless.

This seems like it would be such a simple task but I'm getting nowhere fast. Does anyone know where to find a simple step by step tutorial or can someone detail the steps required to accomplish this?

UPDATE: So, I've given up on the google API to access scripts. I can use all of the drive api calls but it seems it's not possible to call my own script via the API. Here is my ultimate goal:

A google script to zip files:

function doGet(e) {
  var fileIds = [];
// hard code a couple of file id's just for simplicity now.
  fileIds.push('0BzoYw0RgVVOvU0RqdTFrcGdSSzA');
  fileIds.push('0BzoYw0RgVVOvNEJNcWpmSkNxNTA');  
  return ContentService.createTextOutput(zipping(fileIds));
}

function zipping(fileIds) {
  var zipfilename = "sample2.zip";
  var blobs = [];
  var mimeInf = [];
  var accesstoken = ScriptApp.getOAuthToken();
  fileIds.forEach(function(e) {
      try {
          var file = DriveApp.getFileById(e);
          var mime = file.getMimeType();
          var name = file.getName();
      } catch (er) {
          return er
      }
      var blob;
          blob = UrlFetchApp.fetch("https://www.googleapis.com/drive/v3/files/" + e + "?alt=media", {
              method: "GET",
              headers: {"Authorization": "Bearer " + accesstoken},
              muteHttpExceptions: true
          }).getBlob().setName(name);
      blobs.push(blob);
  });
  var zip = Utilities.zip(blobs, zipfilename);
  return DriveApp.createFile(zip).getId(); // return the file id of the new zip file.
}

Publish -> Deploy as web app...

Select "New"

Select "Me"

Select "Only Myself"

Click "Deploy" and authorize the access as requested.

Call the web app with curl from command line to test:

curl -L https://script.google.com/macros/u/0/s/### script id ###/exec

Get html containing the error: "Sorry, unable to open the file at this time."

Change permissions so anyone can execute the app.

Same error. Even entering the url in the browser as the same user gives the same error.

At this point I think I have to admit defeat and find a solution other than Google.

------------------ UPDATE 2 --------------------

Apparently the error:

"Sorry, unable to open the file at this time. Please check the address and try again."

I think this is some oddball problem with Google. The same script works in some accounts but not others. I see from searching the web, others randomly get this error and there is no definitive solution. If anyone knows one, please let me know.

----------------- UPDATE 3 --------------------

This error is an oddball problem with Google. Apparently a new script in some accounts will get this error until the next day. I have verified this several times so if this happens to you, wait a day and try executing it again. The good thing is that after Google finally is able to "open the file" you can make any changes you want, including additional script files to that project, and it updates instantly.

However, a new project will have to wait until the next day so pre create any you think you might want and a couple extra a day ahead of time.

1 Answer 1

6

How about this sample script? When you deploy Web Apps, please copy and paste the URL. And please use it to $url of the following script.

How to deploy Web Apps.

  • On the Script Editor
    • File
    • -> Manage Versions
    • -> Save New Version
    • Publish
    • -> Deploy as Web App
    • -> "Project version:" is latest one or create as New.
    • -> At "Execute the app as", select "your account"
    • -> At "Who has access to the app", select "Anyone, even anonymous"
    • -> Click "Deploy"
    • -> Copy "Current web app URL"
    • -> Click "OK"

Sample script :

<?php
$url = 'https://script.google.com/macros/s/#####/exec';
$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);
?>

Result :

hello world!

References :

If I misunderstand your question, I'm sorry.

Sign up to request clarification or add additional context in comments.

23 Comments

Hi again Tanaike, I believe I need to do this via the API rather than as a web app due to security issues. I will be sending a list of file id's then the script you helped me with will create a zip file and return the id of the zip file. I think I am good with everything except getting the API authentication working. This is the best documentation I can find but I have been unsuccessful... developers.google.com/apps-script/guides/rest/quickstart/php It does not even explain where to get the script-php-quickstart.json file...
@Tim Duncklee I'm sorry. I misunderstood your question. About using Execution API, script-php-quickstart.json is created when the script is run. After Step 1 and Step 2, when you run the script as Step 3, you can see the created script-php-quickstart.json. script-php-quickstart.json has refresh token and access token.
@Tim Duncklee If you are using the default sample script, you can see script-php-quickstart.json at ~/.credentials/.
@Tim Duncklee Would it be possible to solve your problem? Please let me know if there is anything I can do.
OK. I've given up and decided it's simply not possible to call a google script with the google API. I'll try to get it working as a web app and call it with CURL. However, I cannot allow "Anyone" access so I expect I'll encounter the same impossible authentication issues we've encountered with the API. At this point it's looking like we may have to use something other than google drive for our files. If you have a curl example that allows secure access to drive with curl that would really help! :-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.