Cannot name get parameter 'pid' in google scripts get request - google-apps-script

I want to pass a parameter to my google spreadsheet with a get request. The get parameter name is 'pid', which seems to make Google Sheets crash.
My example script just returns the get parameters to the client:
function doGet(e){
try {
return ContentService
.createTextOutput(JSON.stringify({"result":"your parameters", "parameters": e.parameter}))
.setMimeType(ContentService.MimeType.JSON);
} catch(ee){
return ContentService
.createTextOutput(JSON.stringify({"result":"error", "error message": ee}))
.setMimeType(ContentService.MimeType.JSON);
}
}
The following request works:
https://script.google.com/macros/s/<sheet-id-here>/exec?a=1
returns:
{"result":"your parameters","parameters":{"a":"1"}}
But simply changing the get parameter name returns an error:
https://script.google.com/macros/s/<sheet-id-here>/exec?pid=1
returns:
We're sorry, a server error occurred. Please wait a bit and try again.
Any idea what's going on here and how to fix this? Is this a bug? Is there a way to handle this on the server side (can't really change my client code)?

It seems Google has changed reserved parameters. As written in the documentation,
Warning: The following parameter names are reserved by the system and shouldn't be used in URL parameters or POST bodies:
c
sid
Using these parameters may result in an HTTP 405 response with the error message "Sorry, the file you have requested does not exist." If possible, update your script to use different parameter names.
It seems both c and sid parameters are valid now and pid parameter is reserved and throws
Sorry, unable to open the file at this time.
There doesn't seem to be anything that you can do server side.

Related

Google App Script for Tag Manager Container List

I am running a google app script to get a list of containers for my GTM account. I have the follow piece of code:
TagManager.Accounts.Containers.list(parent)
parent = my GTM account ID, I even tried putting as a string, EG "xxxxx". I am sure the GTM account ID is correct.
The following error is returned:
Uncaught Error: Response Code: 404. Message: Not Found.
at fetchContainers (Code:19)
Can anyone please help me?
"Parent" is a path:
Inserting just the ID will give an invalid path, and since the API client tries to retrieve a response from that path an invalid path will result in an error.
You need to use a call like this:
TagManager.Accounts.Containers.list(
'accounts/' + accountId,
{fields: 'container(name,publicId)'}
).container;
You can find a complete and working example here:
http://www.appsscript.it/tutorial/lista-dei-containers-di-google-tag-manager-in-spreadsheet-con-google-apps-script/

Issue with Google-API-PHP Client, getting error when running the quick start script

I am facing an issue with quickstart php script here: https://developers.google.com/drive/v2/web/quickstart/php
When I run the script first time, it executes perfectly and the access token is stored in a file called: drive-php-quickstart.json
When I run the script second time, it gives me the error:
Error start:
Notice: Undefined index: expires_in in \google-api-php-client\src\Google\Client.php on line 485
Fatal error: Uncaught exception 'LogicException' with message 'refresh token must be passed in or set as part of setAccessToken' in
Error end:
My assumption is that access token been saved in the file is not in the right format.
Current format:
ya29.CODE-oN_Bearer36001/_ANOTHER-CODE-ANOTHER_ANOTHER_CODE
As you can see, it does not contain the variable "expires_in"
Any suggestions where I am going wrong ? I am running the script as it is, with no modifications.
I've debugged it.... The person who wrote it made a mistake by not calling json_encode before writing the auth result to the token.json file.
You can fix it by adding json_encode on line 45.
So...
file_put_contents($credentialsPath, $accessToken);
...should be:
file_put_contents($credentialsPath, json_encode($accessToken));
I've submitted feedback so hopefully it'll be fixed.
edit: same issue happens for the token refresh call in that same method
edit2: Here's my related comment in a Github discussion and an answer from Google: https://github.com/google/google-api-php-client/issues/263#issuecomment-186557360
I suggested something along the following lines:
if ($client->isAccessTokenExpired()) {
$refreshToken = $client->getRefreshToken();
$client->refreshToken($refreshToken);
$newAccessToken = $client->getAccessToken();
$newAccessToken['refresh_token'] = $refreshToken;
file_put_contents($credentialsPath, json_encode($newAccessToken));
}
Instead of:
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->refreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, $client->getAccessToken());
}
Google has updated their PHP Quickstart, with an improved method to handle this:
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$client->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
}

URL validation in TCL

I'm trying to download a file over HTTP using http package in TCL. If the given URL is correct then everything is going fine but if the given URL is wrong then it is not throwing any errors. Now my question is that How to validate given URL using TCL?
Thanks in Advance,
Vijay
The http package doesn't throw errors for most failures (except host resolution); you have to check for them with the http::ncode command.
set token [http::geturl $theWrongUrl]
if {[http::ncode $token] >= 400} {
error "http problem: [string range [http::code $token] 13 end]"
}
(This code will leak a token; it omits the http::cleanup call…)

how can I access HTTP GET variables in Google Apps Script?

I am trying to access the HTTP GET variables in a Google Apps Script (Like PHPs $_GET).
I tried several techniques:
Plain java script (window.location), which obviously is not supported
Googles own Service through SitesApp.getActivePage().getUrl(). Gives
me the URL, but without the variable string. On second thought this
might make sense.
e.parameter.[key] This gives me the following error: TypeError: Cannot read property "parameter" from undefined.
I'd really appreciate some pointers or a working example, thanks.
As per the comment from Srik I tried the following:
function doGet(e) {
UserProperties.setProperty("bottleid", e.parameter.id);
return HtmlService.createTemplateFromFile('showbottledata').evaluate();
}
Which gives me the mentioned
TypeError: Cannot read property "parameter" from undefined. (line 2)
error.
First, you need to define your doGet in such a way that it accepts URL parameters
function doGet(e) {
....
}
When you call your URL using URL parameters, say parm1=val1, use
var parm1 = e.parameter.parm1 ;

"incompatible type passed in as parameter"

I receive this error message:
"incompatible type passed in as parameter"
after my program has been idle for a period(usually after returning from a break but sometimes sooner).
Are there variables that time-out after a period? And, how can I handle the error better rather than receiving this mysterious error message?
Has anyone experienced this when mistakenly identical Id's on different elements?
For example:
var lbl2=app.createLabel("Test1")
.setId('idlabel');
var lbl3=app.createLabel("Test2")
.setId('idlabel');
and then resetting the value with:
app.getElementById("idlabel").setText('changed');
Thank You -JS
Sorry, the original answer below is incorrect...Google Apps Script can successfully pass objects as parameters and this does not cause "Incompatible Type Error Message". -JS
I found the error after much very difficult troubleshooting: Google Apps Script cannot reliably parse parameters passed to functions when the parameter is an object. For Example:
function projEditNew(e){
var app = UiApp.getActiveApplication();
var project = new projectO();
project['op']='insert';
var title="New Project"; /*This causes incompatible type error Pass into project properties or other instead */
projEdit(project,app,title);
return app;
}