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.

ziganotschka
  • 25,866
  • 2
  • 16
  • 33
Tim Duncklee
  • 1,420
  • 1
  • 23
  • 35

1 Answers1

5

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.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • 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... https://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 Nov 13 '17 at 21:26
  • @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. – Tanaike Nov 14 '17 at 00:01
  • @Tim Duncklee If you are using the default sample script, you can see ``script-php-quickstart.json`` at ``~/.credentials/``. – Tanaike Nov 14 '17 at 01:01
  • @Tim Duncklee Would it be possible to solve your problem? Please let me know if there is anything I can do. – Tanaike Nov 17 '17 at 08:22
  • 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! :-) – Tim Duncklee Nov 17 '17 at 22:48
  • @Tim Duncklee For example, how about using Web Apps by including a password in the URL query? When the number of strings for the password is large, the security is a bit high. By this, only users who know the password can access the Web Apps. When I use Web Apps as a simple way, I use this. By the way, can I ask you about the curl of the web app? – Tanaike Nov 17 '17 at 23:14
  • I realized I do not need to worry about this web app having security as it only creates files that are not publicly accessible. However, I'm back to being stuck at the "Sorry, unable to open the file at this time. Please check the address and try again." error. I have it working in my personal account but get the error in the G-Suite account. I had this problem before and it mysteriously went away. – Tim Duncklee Nov 18 '17 at 01:12
  • @Tim Duncklee Can I ask you about the condition for deploying Web Apps? Those are the values of ``Execute the app as:`` and ``Who has access to the app:``. – Tanaike Nov 18 '17 at 01:15
  • I have tried every setting possible including Execute as Me and Who has access, Anyone, even anonymous. – Tim Duncklee Nov 18 '17 at 02:03
  • @Tim Duncklee ``Execute the app as:`` and ``Who has access to the app:`` are ``Me`` and ``Anyone, even anonymous``, respectively. And at that time, it requires to redeploy Web Apps as a new version. If you have already done this condition, can I see your script on the script editor if you can share? If you cannot do it, don't worry. – Tanaike Nov 18 '17 at 02:18
  • Even the simplest script give the error in the G-Suite paid account: function doGet() { return ContentService.createTextOutput("hello world!"); } – Tim Duncklee Nov 18 '17 at 02:24
  • I am using the permissions you say and deploying as new. I think this is a google bug. – Tim Duncklee Nov 18 '17 at 02:24
  • @Tim Duncklee You could do that at no paid account. But you cannot do that at paid account. Is my understanding of your situation correct? – Tanaike Nov 18 '17 at 02:27
  • correct. However, I've been through this before and randomly it works in paid account. Even when nothing has changed. No re-deployment. – Tim Duncklee Nov 18 '17 at 02:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/159271/discussion-between-tim-duncklee-and-tanaike). – Tim Duncklee Nov 18 '17 at 02:28
  • @Tim Duncklee I could understand your situation. I would like to look for the information for leading to the solution. Can you give me a time to do? When I need more information of your situation, I will tell you. If I found it or other workaround, I will report you. – Tanaike Nov 18 '17 at 02:31
  • I figured out the issue with the "Sorry, unable to open the file at this time. Please check the address and try again." error. See my update 3 above. – Tim Duncklee Nov 18 '17 at 19:15
  • @Tim Duncklee Thank you for your additional information. I don't know whether this is useful. But about the status of Google, you can see it at https://www.google.com/appsstatus#hl=en-GB&v=status – Tanaike Nov 18 '17 at 23:17
  • I've accepted your answer as it did get me further towards my goal. I've posted a new question specific to my latest issue. https://stackoverflow.com/questions/47372805/how-to-post-to-google-script-with-curl-from-php-and-return-text – Tim Duncklee Nov 19 '17 at 01:18
  • @Tim Duncklee Thank you for the information. I will check the question. I cannot still find the difference between paid account and no paid account for deploying Web Apps. I'm sorry. – Tanaike Nov 19 '17 at 01:58
  • @Tim Duncklee At G Suite, it seems that only the administrator can deploy Web Apps. Can you deploy it as the administrator? If you have already done it, I'm sorry. – Tanaike Nov 19 '17 at 02:57
  • Yes. I am administrator. I've also tried in my personal account. – Tim Duncklee Nov 19 '17 at 03:01
  • @Tim Duncklee Thank you for your reply. I would like to look for other information. – Tanaike Nov 19 '17 at 03:07