I have an issue where other uses of my Google apps script's url is getting changed. Due to this issue they are not able to open the html page.
Original url "https://script.google.com/a/macros/google.com/s/abcxyz-kaskasdb/exec?v=applyleave"
changed url "https://script.google.com/macros/s/abcxyz-kaskasdb/exec?v=applyleave"
I realize "/a" and "/google.com" is getting removed some how.
How can I fix this issue.
Here is my code that is rendered:-
function include(filename)
{
return HtmlService.createHtmlOutputFromFile(filename).getContent();
}
function render(file,argsObject){
var tmp = HtmlService.createTemplateFromFile(file);
if(argsObject){
var keys = Object.keys(argsObject);
keys.forEach(function(key){
tmp[key] = argsObject[key];
});
}
return tmp.evaluate();
}
And here's the code for the server which should accept POST requests:
var Route = {};
Route.path = function (route,callback){
Route[route] = callback;
}
function doGet(e) {
Route.path("applyleave",leaveApply)
Route.path("leaveroster",leave_Roster)
if (Route[e.parameters.v]){
return Route[e.parameters.v]();}
else {
html = HtmlService.createTemplateFromFile('home');
return html.evaluate();
}
}
The error received from other side is this :-
Can anyone explain and provide solution?
There is nothing wrong with the deployment URL getting changed - it is common for Google to perform this redirection.
The issue is can be rather a permission issue. Make sure you deploy the WebApp as "Anyone, even anonymous".
However, currently I am experiencing the same behavior like you due to a multiply reported recent bug:
https://issuetracker.google.com/72798634
https://issuetracker.google.com/165350842
https://issuetracker.google.com/166010550
https://issuetracker.google.com/166320373
https://issuetracker.google.com/167692852
https://issuetracker.google.com/169349069
Please refer to this Github repo.
After the first deployment, you need to make further deployments on the same version by clicking on "Manage Deployment" and then selecting the version to "New version"
enter image description here
Related
I am currently writing a Google Apps Script inside a Google Sheet to read data from a list of spreadsheets (spreadsheet url is provided by the user). However, I cant seems to find a way to check if the url is valid or if user have access to the spreadsheet or not before calling SpreadsheetApp.openByUrl().
I have written the following code to "validate" the url:
for(int i = 0; i < urls.length; i++) {
let spreadsheet = null
try {
spreadsheet = SpreadsheetApp.openByUrl(urls[i]);
} catch (e) {
continue;
}
// Continue to do other stuff to read data from spreadsheet...
}
This however has an issue, it was able to catch the first few 'You do not have permission to access the requested document.' exception. But after a certain number of exception had occur, I would get a permenant error that cant be caught, stopping the script all together.
Is there a better way to do this?
Minimal reproducible example:
Create 3 google sheet using different google account
Using a different google account, create a google sheet and add the following code into Code.gs
function myFunction() {
// Put any 3 real spreadsheet url that you do not have access to
let urls = [
"https://docs.google.com/spreadsheets/d/1gOyEAz0amm4RghpE4B7f26okU3PG3vWZkrfiC-SBlbw/edit#gid=0",
"https://docs.google.com/spreadsheets/d/1Oia7ADu5BmYroUq1SLyDMHTJowrwSXOhCEyNO3nXmMA/edit#gid=0",
"https://docs.google.com/spreadsheets/d/1HE_IXURpBr_FJN--mwLo6k9gih07ZEtDGBqYSk6KgiA/edit#gid=0",
]
urls.forEach(url => {
try {
SpreadsheetApp.openByUrl(url)
} catch (e) {
console.log("Unable to open this spreadsheet")
}
})
}
function onOpen() {
SpreadsheetApp.getUi().createMenu("Test").addItem("myFunction", "myFunction").addToUi()
}
Run the function once in the apps script panel and authorize the application
Refresh this google sheet
Wait for the Custom Menus to show up and press "Menu" > "myFunction"
As you can see, the openByUrl() call is sitting inside the try catch block, however when you run the function through custom menu, you will still get "Error: You do not have permission to access the requested document.".
Executions Log:
From your question, I thought that your situation might be due to the specification or a bug of SpreadsheetApp.openByUrl. If my understanding is correct, in order to avoid this issue, how about putting the method for checking whether the file can be used before SpreadsheetApp? In your script, how about the following modification?
From :
SpreadsheetApp.openByUrl(url)
To:
var fileId = url.split("/")[5];
var file = DriveApp.getFileById(fileId);
spreadsheet = SpreadsheetApp.open(file);
In this modification, the file is retrieved with DriveApp.getFileById(fileId). When fileId cannot be used, an error occurs. But in this case, try-catch can be correctly worked. By this, the issue of SpreadsheetApp doesn't occur.
Here is my script, which basically iterate through drive folder, and put the file's blob as a attachment to some page on google site. It was working fine till day before yesterday, suddenly stop working after that.
function myFunction() {
var testpage = SitesApp.getSiteByUrl(siteURL).getChildByName("test");
var photofolder = DriveApp.getFolderById(folder_ID);
var filesinpf = photofolder.getFiles();
while(filesinpf.hasNext()){
var file = filesinpf.next();
var fblob = file.getBlob();
testpage.addHostedAttachment(fblob); //This line generating an error
}
}
Please help!
I had the same problem yesterday.
I have been working with this for a week, and yesterday I got an internal error in the last line:
function myFunction() {
var myFolder = DriveApp.getFolderById
("0B-ZOMOQnNEDOU9sWEV5SzlXVTQ");
var myFile =
myFolder.getFilesByName("Data.txt").next();
var myBlob = myFile.getBlob();
var myPage =
SitesApp.getSiteByUrl("https://sites.google.com/site/
demo ").getChildByName
("home/demoFileCabinet");
myPage.addHostedAttachment(myBlob);
}
Maybe a problem in Google Sites??. The code is correct.
I created an Issue Tracker too.
You can reproduce this error by using the sample code provided by google:
https://developers.google.com/apps-script/reference/sites/page#addHostedAttachment(BlobSource)
I created an enterprise support Ticket.
Keep you updated: [Case #14128120] Google Sites addHostedAttachment() not working
I got the following response from google:
Let me confirm that this is an issue on our end, filed with issue ID #68842220.
Please notice that our Engineering Team has already found the root cause for this.
At the moment, still, I can't confirm you when it will be fixed, but let me provide you with a simple workaround that will work while we wait for the fix: swap the "domain.com" and the "macros" parts of the URL when entering the URL in the dialog.
Sample, for url:
https://script.google.com/a/domain.com/macros/s/AKfycbwJfGpXIiWHfsCi-j66RuPMNx6kTFsdjYIbNOyufZptGA1tirm6/exec
try
https://script.google.com/a/macros/domain.com/s/AKfycbwJfGpXIiWHfsCi-j66RuPMNx6kTFsdjYIbNOyufZptGA1tirm6/exec
I am testing the functionality of UrlFetchApp and passing data from a Form and its Spreadsheet. I know it's possible to do this another way, however I am testing the functionality of UrlFetchApp (first time using it) within google scripts themselves, and want to get it to work with this method.
Here's the scenario I got, add a bound script to a Form App as so:
function makeRequest()
{
var webAppUrl = "https://script.google.com/macros/s/WebAppID/exec";
var auth = ScriptApp.getOAuthToken();
var header = { 'Authorization': 'Bearer ' + auth };
var options = { 'method':'post', 'headers':header };
var resp = UrlFetchApp.fetch(webAppUrl, options);
Logger.log(resp);
}
Add a bound script to the attached spreadsheet:
function doPost()
{
var ss = SpreadsheetApp.openById('ssID');
var name = ss.getName();
return ContentService.createTextOutput(name);
}
And then publish this second script attached to the sheet as a web app with only myself to have access.
Currently the above code does not work. The following error appears on the Form side of the script:
Request failed for
https://script.google.com/macros/s/WebAppID/exec
returned code 401. Truncated server response:
Unauthorized Unauthorized Error 401
(use muteHttpExceptions option to examine full response) (line
12, file "Code")
Fails on the UrlFetchApp line of code.
However, if I remove the header option, then publish the web app for public use, this works just fine. Obviously this is not wanted.
What am I missing regarding authentication between scripts that I own?
Side Notes:
Anyone know why SpreadsheetApp.getActiveSheet() doesn't work when run in this fashion? That script is directly bound to a google sheet, so kind of odd.
Ok, found the answer to my own question. It was quite simple really. Needed to add the following scope to my project for accessing a spreadsheet:
https://www.googleapis.com/auth/drive
The easiest way I found to do this is to add a simple function like this and call it:
function authorizeDrive()
{
var forScope = DriveApp.getRootFolder();
}
Doesn't need to return or do anything, just call any method from the DriveApp. Once run, it'll then popup with a dialogue for authorization. Don't even need to call this every time you do your main method calls. Don't even need to leave it coded in the script either. I wonder if there is way to just simple add the scope you need to a project from a properties window (I didn't find any). Or perhaps a way to pass a parameter along with UrlFetchApp regarding what scope need authorized.
Buy anyhow this still wasn't too bad.
Regarding my side note, I still haven't found a reason as to why SpeadsheetApp.getActiveSheet() returns null or undefined. I have to open by ID or URL, which is a pain. Especially since this is a container bound script. Also noticed that Logger.log() doesn't actually add anything to the Logger when run in this manner. If anyone could still shed some light on either of these, that would be great.
You need to get the 'Spreadsheet' object first.
SpeadsheetApp.getActive().getActiveSheet()
However, if you are creating an add-on menu you can use 'SpreadsheetApp.getActiveSheet()'
function myFunction() {
var lastRow = SpreadsheetApp.getActiveSheet().getLastRow();
var range = SpreadsheetApp.getActiveSheet().getRange(lastRow, 1, 1, 26);
SpreadsheetApp.setActiveRange(range);
}
function onOpen(e) {
SpreadsheetApp.getUi().createAddonMenu()
.addItem('showLastRow', 'myFunction')
.addToUi();
}
I've read a lot of other people with half of this problem, and tried their solutions, but I can't get anywhere with it.
I first ran the example tutorials from google here:
https://campus.codeschool.com/courses/discover-drive/
Then I ran the example code from here:
https://developers.google.com/picker/docs/index
Running those codes with my API and client keys (I've even created new keys, tried using only the part before the .apps.googleusercontent.com for the client key as I've seen in some other example code, etc.) and I get the same result.
The page loads and asks for permission to access google drive for an account As soon as I give it permission it reverts to the following error:
Jconsole Error
I have a useless window error that says:
There was an error!
The API developer key is invalid.
Now Other people have said the error was normal, and that their code still worked. Mine on the other hand does not work, AND it gives me that error.
<script>
var clientId = '249642562982-8ss843cvik6r1rrm94i1kt5cf4jf201c.apps.googleusercontent.com';
var developerKey = 'AIzaSyBeDBmIqzCYCNvrpSwXcLz6ido_qGZL6sg';
var accessToken;
function onApiLoad() {
gapi.load('auth', authenticateWithGoogle);
gapi.load('picker');
}
function authenticateWithGoogle() {
window.gapi.auth.authorize({
'client_id': clientId,
'scope': ['https://www.googleapis.com/auth/drive']
}, handleAuthentication);
}
function handleAuthentication(result) {
if(result && !result.error) {
accessToken = result.access_token;
setupPicker();
}
}
function setupPicker() {
var picker = new google.picker.PickerBuilder()
.setOAuthToken(accessToken)
.setDeveloperKey(developerKey)
.addView(new google.picker.DocsUploadView())
.enableFeature(google.picker.Feature.MULTISELECT_ENABLED)
.setCallback(myCallback)
.build();
picker.setVisible(true);
}
function myCallback(data) {
if (data.action == google.picker.Action.PICKED) {
alert(data.docs[0].name);
} else if (data.action == google.picker.Action.CANCEL) {
alert('goodbye');
}
}
</script>
<script src="https://apis.google.com/js/api.js?onload=onApiLoad"></script>
Here are my keys
This is driving me nuts! It feels like I've tried everything and nothing is working. Why would google spend time to create a tutorial site, with full-on video lessons that check your code's validity if the flippin' resulting code doesn't work!? Take down the videos and example code if it is broken! I've wasted all day on this.
Can anyone help me figure out what's going on here?
References:
How to upload file on google drive from my website?
Using Google Drive in an iFrame doesn't work
Google Drive Picker - Developer Key is Invalid Error
Google Drive API OAuth 2.0; Error: origin_mismatch
I would recommend to use this example. I have also tested this example, it is working for me. You can edit the scope according to your requirements.
My Goal: Changes in Google Drive => Push Notification to https://script.google.com/a/macros/my-domain/... => App is pushed to take action.
I don't want to setup an middle Webhook agent for receiving notification. Instead, let the Web App (by Google Script) to receive it and be pushed directly.
Since the relevant function is quite undocumented (just here: https://developers.google.com/drive/web/push) , below is the code I tried but failure.
1. Is above idea feasible??
2. My code doPost(R) seems cannot receive notification (R parameter) properly. Anyway, no response after I change the Google Drive. Any problem? (I have tried to log the input parameter R so as to see its real structure and decide if the parameter Obj for OAuth is the same as normal Drive App, but error occur before log)
function SetWatchByOnce(){
var Channel = {
'address': 'https://script.google.com/a/macros/my-domain/.../exec',
'type': 'web_hook',
'id': 'my-UUID'
};
var Result = Drive.Changes.watch(Channel);
...
}
function doPost(R) {
var SysEmail = "My Email";
MailApp.sendEmail(SysEmail, 'Testing ', 'Successfully to received Push Notification');
var Response = JSON.parse(R.parameters);
if (Response.kind == "drive#add") {
var FileId = Response.fileId;
MyFile = DriveApp.getFolderById(FileId);
...
}
}
function doGet(e) {
var HTMLToOutput;
var SysEmail = "My Email";
if (e.parameters.kind) {
//I think this part is not needed, since Push Notification by Drive is via Post, not Get. I should use onPost() to receive it. Right?
} else if (e.parameters.code) {
getAndStoreAccessToken(e.parameters.code);
HTMLToOutput = '<html><h1>App is successfully installed.</h1></html>';
} else { //we are starting from scratch or resetting
HTMLToOutput = "<html><h1>Install this App now...!</h1><a href='" + getURLForAuthorization() + "'>click here to start</a></html>";
}
return HtmlService.createHtmlOutput(HTMLToOutput);
}
....
Cloud Functions HTTP trigger(s) might also be an option ...
(which not yet existed at time of this question). this just requires setting the trigger URL as the notification URL, in the Google Drive settings - and adding some NodeJS code for the trigger; whatever it shall do. one can eg. send emails and/or FCM push notifications alike that. that trigger could also be triggered from App Script, with UrlFetchApp and there is the App Script API. one can have several triggers, which are performing different tasks (App Script is only one possibilty).
Cicada,
We have done similar functions to receive webhooks/API calls many times. Notes:
to get R, you need: var Response = R.parameters and then you can do Response.kind, Response.id, etc.
Logger will not work with doGet() and doPost(). I set it up a write to spreadsheet -- before any serious code. That way I know if it is getting triggered.