问题
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:
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 :
- Web Apps
- cURL Functions
If I misunderstand your question, I'm sorry.
来源:https://stackoverflow.com/questions/47255694/how-to-do-simple-google-script-api-with-php