UrlFetchApp.fetch(url) fails - google-apps-script

I have a bit of GAS code that is failing when executing UrlFetchApp.fetch(url) - I think.
Are there limits to the character length of a url when used in UrlFetchApp.fetch(url). My function is failing and I suspect that it has something to do with the length of the url. It is over 100 chars.
The code below refers...
function uploadToDrive(url, folderid, filename, fileDesc) {
var msg = '';
try {
var response = UrlFetchApp.fetch(url);
} catch(err) {
};
if (response.getResponseCode() === 200) {
var folder = DriveApp.getRootFolder();
if (folderid) {
folder = DriveApp.getFolderById(folderid);
}
var blob = response.getBlob();
var file = folder.createFile(blob);
file.setName(filename);
file.setDescription(fileDesc);
var headers = response.getHeaders();
var content_length = NaN;
for (var key in headers) {
if (key.toLowerCase() == 'Content-Length'.toLowerCase()) {
content_length = parseInt(headers[key], 10);
break;
}
}
var blob_length = blob.getBytes().length;
msg += 'Saved "' + filename + '" (' + blob_length + ' bytes)';
if (!isNaN(content_length)) {
if (blob_length < content_length) {
msg += ' WARNING: truncated from ' + content_length + ' bytes.';
} else if (blob_length > content_length) {
msg += ' WARNING: size is greater than expected ' + content_length + ' bytes from Content-Length header.';
}
}
msg += '\nto folder "' + folder.getName() + '".\n';
}
else {
msg += 'Response code: ' + response.getResponseCode() + '\n';
}
return file.getUrl();
}

That link generates a response code 404, but you set the response variable only if the fetch method is successful in your try block. Try validating the response variable before assuming it has properties and methods to access:
function fetching() {
var url = "<some url resource>";
try {
var response = UrlFetchApp.fetch(url);
} catch (err) {
// why catch the error if you aren't going to do anything with it?
Logger.log(err);
} finally {
if (response && response.getResponseCode() === 200) {
Logger.log("Should be 200, got " + response.getResponseCode());
} else {
Logger.log("Fetching Failed: Exception thrown, no response.");
}
}
}
However, I would go farther and guarantee a response from UrlFetchApp.fetch. This is done with the "muteHttpExceptions" paramater set to true.
function fetching() {
//dns exception
//var = "googqdoiqijwdle.com"
//200
var url = "google.com";
//404
//var url = "https://www.jotform.com/uploads/Administrator_System_sysadmin/92960977977584/4480552280228238115/BoE%20Test%20File.docx"
try {
var response = UrlFetchApp.fetch(url, {
muteHttpExceptions: true
});
} catch (err) {
Logger.log(err);
}
if (response) {
response.getResponseCode() === 200 ?
Logger.log("Should be 200, got " + response.getResponseCode()) :
Logger.log("Anything but 200, got " + response.getResponseCode());
}
}
Hope this helped!
Docs: Google Developers

Related

How to use correctly google drive API?

I have developed the following code based on other code I found on the internet, but it does not work.
Can someone help me to understand my issue?
I get an error saying I don't have the right.
What is the problem?
Thank you very much for your support, that will be well appreciated.
var tokenService_ = function(){return ScriptApp.getOAuthToken()};
function exe() {
var listObj = reportTeamDrivePermissions('id of the folder');
console.log(listObj.toString());
}
function reportTeamDrivePermissions(driveId) {
// Reading the Drive permissions
var options = {"supportsAllDrives": true, "fields": 'permissions,nextPageToken', "pageSize":100};
var paramString = Object.keys(options).map(function(key) {
return encodeURIComponent(key) + '=' + encodeURIComponent(options[key]);
}).join('&');
var url = "files/"+driveId+"/permissions";
url = url + (url.indexOf('?') >= 0 ? '&' : '?') + paramString;
url = "https://www.googleapis.com/drive/v3/" + url;
var fetchOptions = {method:"GET",muteHttpExceptions:true, contentType:"application/json", headers:{Authorization:"Bearer "+tokenService_()}};
var response = UrlFetchApp.fetch(url, fetchOptions);
if(response.getResponseCode() != 200){
throw new Error(response.getContentText());
} else {
var PermissionListResource = JSON.parse(response.getContentText());
}
var myPermisionList = PermissionListResource.permissions;
var nextPageToken = PermissionListResource.nextPageToken;
while (nextPageToken != null) {
// Reading the Drive permissions
options = {"supportsAllDrives": true, "fields": 'permissions,nextPageToken', "pageSize":100, 'pageToken': nextPageToken};
paramString = Object.keys(options).map(function(key) {
return encodeURIComponent(key) + '=' + encodeURIComponent(options[key]);
}).join('&');
url = "files/"+driveId+"/permissions";
url = url + (url.indexOf('?') >= 0 ? '&' : '?') + paramString;
url = "https://www.googleapis.com/drive/v3/" + url;
fetchOptions = {method:"GET",muteHttpExceptions:true, contentType:"application/json", headers: {Authorization:"Bearer "+tokenService_()}}
response = UrlFetchApp.fetch(url, fetchOptions)
if(response.getResponseCode() != 200){
throw new Error(response.getContentText());
} else {
var PermissionListResource2 = JSON.parse(response.getContentText());
}
myPermisionList = myPermisionList.concat(PermissionListResource2.permissions);
nextPageToken = myPermisionObject2.nextPageToken;
}
return myPermisionList;
}

Card payment error 508 -Realex payments error number: 61,754

i am using realex payment with iframe
i can load the payment page correctly, but as soon as i hit 'Pay Now'
it return Error: 508
Message: An error has occurred processing your request. Please contact the merchant whose goods or services you are purchasing quoting the following error number: 61,754
(most of time it return correct response string either successful and declined) but sometimes it return above error code)
any idea what is mean and how to solve this issue?
<script type='text/javascript'>
function iRedirect(redirectUrl, arg, value) {
console.log(redirectUrl);
try {
var form = $('<form action="' + redirectUrl + '" method="post">' +
'<input type="hidden" name="' + arg + '" value="' + value + '"></input>' + '</form>');
$('body').append(form);
console.log(form);
$(form).submit();
}
catch (e) {
alert(e.message);
}
}
function displayMessage(evt)
{
var message;
try {
var iOrigin = '<%=ConfigurationManager.AppSettings["RealexResponseDomain"] %>';
if (evt.origin == iOrigin) {
message = evt.data.toString();
console.log(message);
if (message.indexOf("Error") == 0) {
var ErrorJsonStr = message.toString().split(":");
var ErrorJsonStr1 = ErrorJsonStr[1].split("<BR>");
var reDirectPath = "{\"" + ErrorJsonStr[0] + "\"" + ":" + "\"" + ErrorJsonStr1[0] + "\"" + "," + "\"" + ErrorJsonStr1[1] + "\"" + ":" + "\"" + ErrorJsonStr[2] + "\"" + "}";
iRedirect("Response.aspx", "JsonStr", encodeURIComponent(reDirectPath));
}
else {
if (isJson(message) == true) {
var message1 = JSON.parse(message);
//alert(message1);
console.log(message1);
if (message1.hasOwnProperty('pas_uuid')) {
iRedirect("Response.aspx", "JsonStr", encodeURIComponent(message.toString()));
}
else {
//check if this transaction is already exist
//do redirect
//alert("not pas_uuid" + message1);
console.log("not pas_uuid" + message1);
}
}
}
//get message and check result
}
else {
console.log("not data");
}
}
catch (err) {
console.log(err.message);
}
}
function isJson(str) {
try {
JSON.parse(str);
}
catch (e)
{
console.log(e.message);
return false;
}
return true;
}
if (window.addEventListener) {
// For standards-compliant web browsers
window.addEventListener("message", displayMessage, false);
}
else {
window.attachEvent("onmessage", displayMessage);
}
</script>
There are two major scenarios where this message may be displayed on the HPP.
There was a temporary issue with the HPP Sandbox environment.
The Merchant ID and Account you are using has 3D Secure 1 enabled, but you used a non-3D Secure enabled test card.
If you think it wasn't either of these issues, please provide our Support Team with an example Order ID of a transaction where this occurred and they will be able to look at the logs in more detail.

Google Service Account Delegation 404 error

I am attempting to authenticate with a service account to work on behalf of a user account on the domain. I have delegated admin access and added to the GSuite console. I can get an access token with the below but the making batch requests to copy drive files returns "code: 404, message: 'File not found:". The below code is writted in Google Apps Script. Am I missing something form the process to creating and authenticating the service account?
var CREDENTIALS = {
private_key: "-----BEGIN PRIVATE KEY----- XXXXXXX \n-----END PRIVATE KEY-----\n",
client_email: "XXXXXX#fXXXXXX.iam.gserviceaccount.com",
client_id: "1XXXXXXXXXXXXXXXX",
user_email: "XXXXX#XXXX.XXX.XXX",
scopes: ["https://www.googleapis.com/auth/drive","https://www.googleapis.com/auth/spreadsheets","https://www.googleapis.com/auth/userinfo.email","https://www.googleapis.com/auth/script.external_request"]
};
function oAuthToken(){
var url = "https://www.googleapis.com/oauth2/v3/token";
var header = {
alg: "RS256",
typ: "JWT",
};
var now = Math.floor(Date.now() / 1000);
var claim = {
iss: CREDENTIALS.client_id,
sub: CREDENTIALS.user_email,
scope: CREDENTIALS.scopes.join(" "),
aud: url,
exp: (now + 3600).toString(),
iat: now.toString(),
};
var signature = Utilities.base64Encode(JSON.stringify(header)) + "." + Utilities.base64Encode(JSON.stringify(claim));
var jwt = signature + "." + Utilities.base64Encode(Utilities.computeRsaSha256Signature(signature, CREDENTIALS.private_key));
var params = {
method: "post",
payload: {
assertion: jwt,
grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer",
},
};
var res = UrlFetchApp.fetch(url, params).getContentText();
return JSON.parse(res)
}
The batch process is a bit rough but this is the gist of it.
var request={
batchPath:
requests:[]
}
var backoff =0
function batch(request) {
var oAuth=oAuthToken().access_token
var url ='https://www.googleapis.com/'+request.batchPath
var body =request.requests
if(body.length<1){
return []
}
var boundary = 'xxxxxxxxxx';
var contentId = 0;
var data = '--' + boundary + '\r\n';
for (var i in body) {
if(typeof body[i]=='object'){
data += 'Content-Type: application/http\r\n';
data += 'Content-ID: ' + ++contentId + '\r\n\r\n';
data += body[i].method + ' ' + body[i].endpoint + '\r\n';
data += body[i].requestBody ? 'Content-Type: application/json; charset=utf-8\r\n\r\n' : '\r\n';
data += body[i].requestBody ? JSON.stringify(body[i].requestBody) + '\r\n' : '';
data += "--" + boundary + '\r\n';
}
}
var parseBatchRes = function(res) {
var splittedRes = res.split('--batch');
return splittedRes.slice(1, splittedRes.length - 1).map(function(e) {
return {
contentId: Number(e.match(/Content-ID: response-(\d+)/)[1]),
status: Number(e.match(/HTTP\/\d+.\d+ (\d+)/)[1]),
object: JSON.parse(e.match(/{[\S\s]+}/)[0]),
};
});
};
var payload = Utilities.newBlob(data).getBytes();
var head = {Authorization: 'Bearer ' + oAuth}
var options = {
method: 'POST',
contentType: 'multipart/mixed; boundary=' + boundary,
payload: payload,
headers: head,
muteHttpExceptions: false
};
var complete=false;
var finalResponse=[];
for (var n=0; n<=backoff; n++) {
if(complete){
break;
}
var complete = true
console.log('backoff',n);
var response =UrlFetchApp.fetch(url, options).getContentText();
for(var j=0;j<response.length;j++){
if(response[r].status!=200){
var complete = false
}
}
}
}
Add the supportsAllDrives = true query parameter to the request.
The parameters indicates whether the requesting application supports both My Drives and shared drives and the default value for this is false.
Reference
Drive API Parameters

Sending multiple JSON request to server under one request nodejs

I have some files downloaded and I wanna push them to a server. Every time I try to push the file I get the error
Error: Can't set headers after they are sent.
I have a for loop that reads all the files and parse them and after that I want to send them to the server one by one.
app.get('/dataparser', function(req, res) {
var fs = require('fs');
var obj;
var jsonGis = new Array();
var jsonArray;
var filePaths = [];
const downloadFolder = './sampletest/';
var mtimes = {};
var reloadTimes = 10000;
fs.readdir(downloadFolder, (err, files) => {
files.forEach(file => {
filePaths.push("sampletest/" + file);
});
})
var execFunction = function() {
for (var i = 0; i < filePaths.length; i++) {
parseFile(filePaths[i], mtimes[filePaths[i]]);
}
};
execFunction();
setInterval(execFunction, reloadTimes);
function parseFile(fileName, lastModifiedTime) {
fs.stat(fileName, function(err, fd) {
for (var i=0, len = filePaths.length; i<len; i++) {
if (fd.mtime !== lastModifiedTime) {
mtimes[fileName] = fd.mtime;
fs.readFile(filePaths[i], function(err, data) {
if (err) {
return console.error(err);
}
obj = JSON.parse(data);
jsonGis.push('"Person1"');
jsonGis.push('"' + obj.pages[1].answers[2].values[0] + '"');
jsonGis.push('"person2"');
jsonGis.push('"' + obj.pages[1].answers[0].values[0] + '"');
jsonGis.push('"codewals"');
jsonGis.push('"42343GSDS"');
jsonGis.push('"geometry":{');
jsonGis.push('"x":' + obj.pages[1].answers[4].values[0].coordinates.latitude + ',');
jsonGis.push('"y":' + obj.pages[1].answers[4].values[0].coordinates.longitude);
var str = "[{ " + jsonGis[0] + jsonGis[1] + jsonGis[2] + ": " + jsonGis[3] + "," + jsonGis[4] + ": " + jsonGis[16] + "}}]"
//pushing to the server
console.log("check here");
console.log(str);
var qs = require("querystring");
var http = require("http");
var options = {
"method": "POST",
"hostname": "twst2.gtw.com",
"port": null,
"path": "localpath/",
"headers": {
"accept": "application/json",
"content-type": "application/x-www-form-urlencoded",
}
};
var req = http.request(options, function(res) {
var chunks = [];
res.on("data", function(chunk) {
chunks.push(chunk);
});
res.on("end", function() {
var body = Buffer.concat(chunks);
console.log(body.toString());
});
});
req.write(qs.stringify({features: str}));
res.send("The server was updated");
req.end();
});
}
}
});
};
Just need to send the data to the server from file1.json and then file2.json then file3.json and so on.
Try creating a separate callback function that includes .write, .send, and .end, then let it be the callback for .request.

NodeJS creating JSON using all JSONs uploaded by user

I am trying to make a JSON file using all the JSON files in a directory. Every time a user uploads a new JSON a new combined JSON should be generated. I want the new JSON to have a custom structure hence cant use any libraries. I have the following code:
router.post('/upload', function(req, res) {
var sampleFile;
var bbbid = req.body.bbbid;
DDLFile = req.files.DDLFile;
j++;
DDLFile.mv('/uploads/' + bbbid + '/device' + j + '.json', function (err) {
if (err) {
res.status(500).send(err);
}
else {
res.redirect("fileuploaded");
}
});
var myfiles = [];
var fs = require('fs');
var arrayOfFiles = fs.readdirSync('/uploads/' + bbbid);
arrayOfFiles.forEach(function (file) {
myfiles.push(file);
console.log(myfiles);
});
console.log('No of Files:', myfiles.length);
var files = myfiles.length;
console.log('Files:', files);
console.log('J', j);
var cddl = "{ BBBID:" + bbbid;
if (files == 0) {
cddl = cddl + '}';
console.log('Entered if loop');
}
else {
var i = 0;
/*var obj;
fs.readFile('/uploads/' + bbbid + '/device' + j + '.json', 'utf8', function (err, data) {
if (err) throw err;
obj = JSON.parse(data);
});*/
for (i = 0; i < files; i++) {
console.log('Entered For loop');
console.log('Count:', count);
console.log('Sensor:', sensor);
try{
var obj = fs.readFileSync('/uploads/' + bbbid + '/device' + count + '.json', 'utf8');}
catch(err){
console.log(err);
}
console.log('everything good');
var obj1 = JSON.parse(obj);
console.log('hi');
//JSON.stringify(obj);
var ddl = require('/uploads/' + bbbid + '/device' + count + '.json');
console.log('o');
cddl = cddl + ", {" + obj1.DDL.Sensor.Description.Verbose_Description + ":" + JSON.stringify(ddl) + "}"
JSON.stringify(cddl);
console.log(cddl);
count++;
sensor++;
console.log('Count:', count);
console.log('Sensor:', sensor);
}
cddl = cddl + '}';
JSON.stringify(cddl);
console.log(cddl);
}
});
I want to generate a new cddl everytime a new file is uploaded. Having a lot of problems. Help please!
I see two problems. First instead of this:
var obj = fs.readFileSync('/uploads/' + bbbid + '/device' + count + '.json', 'utf8');}
catch(err){
console.log(err);
}
console.log('everything good');
var obj1 = JSON.parse(obj);
You can write(fix path, if necessary):
var obj1 = require('./uploads/' + bbbid + '/device' + count + '.json')
Then, when you call:
JSON.stringify(cddl);
You're not saving the result anywhere. So you should save it in the place, you need to:
var a = JSON.stringify(cddl);
And when all set, dont forget to write to file back using fs.writeFileSync or async one fs.writeFile.