How can i get the source code after logging in with urlFetchApp? - google-apps-script

I am stucked. I can log in to the website and now after i logged in, i want to get the source code of the url2. Any ideas how i can continue here?
function login() {
var url = "https://www.erevollution.com/en/login";
var payload = {
"email":"test#gmail.com",
"password":"testpassword",
"remember":"on"
};
var options = {
"payload":payload,
"method":"post",
"followRedirects" : false
};
var response = UrlFetchApp.fetch(url, options);
if ( response.getResponseCode() == 200 ) { //could not log in.
var result = "Couldn't login. Username/password is incorrect.";
}
else if ( response.getResponseCode() == 302 ) { //login was successful
var result = "Logged in successfully";
var cookie = response.getAllHeaders()['Set-Cookie'];
var header = { "Cookie":cookie[0] };
var options2 = { "headers": header };
var url2 = "https://www.erevollution.com/tr/market/1/1/1/1";
var response2 = UrlFetchApp.fetch(url2, options2);
}
Logger.log(result);
Logger.log(response2);
}

Issue:
You should be including the cookie in the property headers of the object you are passing as parameter of your call, as you can see in the docs.
Code sample:
var header = { "Cookie": cookie[1] };
var options = { "headers": header };
var url = "https://www.erevollution.com/tr/market/1/1/1/1";
var response = UrlFetchApp.fetch(url, options);
Reference:
UrlFetchApp.fetch(url, params)

I solved the problem by taking my response headers cookies and sorting them as the request headers cookies of the next page (url2) wanted. So i got the right order of cookie (newcookie) for the next page.
function Price() {
var url = "https://www.erevollution.com/en/login";
var payload = {
"email":"test#gmail.com",
"password":"testpassword",
"remember":"on"
};
var options = {
"payload":payload,
"method":"post",
"followRedirects" : false
};
var response = UrlFetchApp.fetch(url, options);
if ( response.getResponseCode() == 200 ) { //could not log in.
var result = "Couldn't login. Username/password is incorrect.";
}
else if ( response.getResponseCode() == 302 ) { //login was successful
var result = "Logged in successfully";
var cookie = response.getAllHeaders()['Set-Cookie'];
for(m=0;m<5;m++){
cookie[m]=cookie[m].substring(0,cookie[m].indexOf(";"));
}
var newcookie=cookie[4]+"; "+cookie[1]+"; "+cookie[2]+";"+cookie[3]+"; "+cookie[0];
var header = { "Cookie":newcookie };
var options2 = { "headers": header };
var url2 = "https://www.erevollution.com/tr/market/1/1/1/1";
var response2 = UrlFetchApp.fetch(url2, options2);
var content = response2.getContentText();

Related

How to update jira status using google script?

I am new here, I am able to find how to update jira summary from google script as below :
function jiraupdate() {
var username = "username";
var password = "password";
var encCred = Utilities.base64Encode(username+":"+password);
var url = "https://{jira_host}/rest/api/2/issue/{jiraidorkey}";
var data = {"update":{"summary":[{"set":"test google script"}]}};
var payload = JSON.stringify(data);
var headers = {
"Accept":"application/json",
"Content-Type":"application/json",
"Authorization":"Basic " + encCred,
};
var options = {
"method":"PUT",
"contentType" : "application/json",
"headers": headers,
"payload" : payload
};
var response = UrlFetchApp.fetch(url, options);
Logger.log(response);
}
thanks to this post here
can anyone show me example how do i update jira status for example from todo to in-progress using google script?
i found it.
function jiraupdate() {
var username = "username";
var password = "password";
var encCred = Utilities.base64Encode(username+":"+password);
var url = "https://{jirahost}/rest/api/2/issue/{jiraidorkey}/transitions";
var data ={"transition":{"id":"221"}};
var payload = JSON.stringify(data);
var headers = {
"Accept":"application/json",
"Content-Type":"application/json",
"Authorization":"Basic " + encCred,
};
var options = {
"method":"POST",
"contentType" : "application/json",
"headers": headers,
"payload" : payload
};
var response = UrlFetchApp.fetch(url, options);
Logger.log(response);
}

CORS Error: The request has been blocked because of the CORS policy

I am writing script to post data to spreadsheet but getting error, when testing the same on Postman:
CORS Error: The request has been blocked because of the CORS policy
Below is my script:
var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1UEzI8a8qVBqzVGuA4AZKs4eb4Y5bqOlIOJWwBDkEKb8/edit#gid=0");
var sheet = ss.getSheetByName('Items'); // be very careful ... it is the sheet name .. so it should match
function doPost(e) {
var action = e.parameter.action;
if (action == 'addItem') {
return addItem(e);
}
}
function addItem(e) {
var date = new Date();
var id = "Item" + sheet.getLastRow(); // Item1
var itemName = e.parameter.itemName;
var brand = e.parameter.brand;
sheet.appendRow([date, id, itemName, brand]);
return ContentService.createTextOutput("Success").setMimeType(ContentService.MimeType.TEXT);
}
You need to return a value from doPost rather than addItem.
This always trips me up, too. If there is no return value, you get a CORS warning and the POST fails.
For this reason, it can be difficult to tell if the script has failed or if there's a problem with the post request. Try wrapping everything in try, catch, finally to ensure you always return a value.
Try this. It's a bit verbose but it should ensure that you definitely get a response to your POST request and that response contains all the information you need to see what's going wrong.
function doPost(e) {
var returnJson = {
success: false,
errors: [],
returnValue: null,
debug: [],
};
try {
if (e.parameter && e.parameter.action) {
var action = e.parameter.action;
if (action == "addItem") {
var itemAdded = addItem(e);
returnJson.debug.push(itemAdded);
}
returnJson.success = true;
} else {
returnJson.debug.push("No value 'action' in e.parameter");
}
} catch (err) {
returnJson.errors.push(err);
} finally {
return ContentService.createTextOutput(
JSON.stringify(returnJson)
).setMimeType(ContentService.MimeType.JSON);
}
}
function addItem(e) {
var returnJson = {
success: false,
errors: [],
returnValue: null,
debug: [],
};
try {
var date = new Date();
var id = "Item" + sheet.getLastRow(); // Item1
var itemName = e.parameter.itemName;
var brand = e.parameter.brand;
var ss = SpreadsheetApp.openByUrl(
"https://docs.google.com/spreadsheets/d/1UEzI8a8qVBqzVGuA4AZKs4eb4Y5bqOlIOJWwBDkEKb8/edit#gid=0"
);
var sheet = ss.getSheetByName("Items"); // be very careful ... it is the sheet name .. so it should match
sheet.appendRow([date, id, itemName, brand]);
returnJson.success = true;
} catch (err) {
returnJson.errors.push(err);
} finally {
return returnJson;
}
}

How to return a redirected URL in Google Sheets

I am working on verifying 301 redirects are functioning properly. I have a Google Sheet of URLs and have a Google script that checks the status being returned:
function getStatusCode(url){
var options = {
'muteHttpExceptions': true,
'followRedirects': false
};
var url_trimmed = url.trim();
var response = UrlFetchApp.fetch(url_trimmed, options);
return response.getResponseCode();
}
Next I am wanting to verify that the URL is being redirected correctly. This is where I'm getting stuck. I tried using a variation of the above code, but I can just return the URL being passed in, not the URL being redirected to, or I just get an error on my Google Sheet. Here is the last bit I tried using (that returns an error).
function getReturnedURL(url) {
var options = {
'method': 'GET',
'muteHttpExceptions': true,
'followRedirects': true
};
var url_trimmed = url.trim();
var returnedUrl = ScriptApp.getService().getUrl(url_trimmed);
var response = UrlFetchApp.fetch(returnedUrl, options);
return response;
}
Any ideas? Or, is this even possible?
So for my situation, I was wanting to pull a URL from a spreadsheet and return the redirected URL in a different column of said spreadsheet. #carlesgg97 put me on the right path, here is what ended up working for me:
function urlProtocol(url){
return URI(url).protocol()
}
function urlHostname(url){
return URI(url).hostname()
}
function getRedirects(url) {
eval(UrlFetchApp.fetch('https://rawgit.com/medialize/URI.js/gh-pages/src/URI.js').getContentText());
var params = {
'followRedirects': false,
'muteHttpExceptions': true
};
var baseUrl = urlProtocol(url) + "://" + urlHostname(url),
response = UrlFetchApp.fetch(url, params),
responseCode = response.getResponseCode();
if(response.getHeaders()['Location']){
var redirectedUrl = getRedirects(baseUrl + response.getHeaders()['Location']);
return redirectedUrl;
} else {
return url;
}
}
You cannot know what the final destination of your request is only from the HTTPresponse obtained.
However, you can use the getRedirects function below to get the URLs that your request would follow upon calling fetch with followRedirects set as true:
function getRedirects(url) {
var params = {
'followRedirects': false,
'muteHttpExceptions': true
};
var followedUrls = [url];
while (true) {
var res = UrlFetchApp.fetch(url, params);
if (res.getResponseCode() < 300 || res.getResponseCode() > 399) {
return followedUrls;
}
var url = res.getHeaders()['Location'];
followedUrls.push(url);
}
}
function test() {
var followedUrls = getRedirects('http://mail.google.com/mail/u/0/#inbox/');
Logger.log(followedUrls);
}
In this case, running test will return:
[http://mail.google.com/mail/u/0/#inbox/,
https://mail.google.com/mail/u/0/,
https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1#]
There was a slight edit needed on Billy's answer (https://stackoverflow.com/a/58801197/9758091) to make it work (remove "baseUrl + " from 6th last Line):
function urlProtocol(url){
return URI(url).protocol()
}
function urlHostname(url){
return URI(url).hostname()
}
function getRedirects(url) {
eval(UrlFetchApp.fetch('https://rawgit.com/medialize/URI.js/gh-pages/src/URI.js').getContentText());
var params = {
'followRedirects': false,
'muteHttpExceptions': true
};
var baseUrl = urlProtocol(url) + "://" + urlHostname(url),
response = UrlFetchApp.fetch(url, params),
responseCode = response.getResponseCode();
if(response.getHeaders()['Location']){
var redirectedUrl = getRedirects(response.getHeaders()['Location']);
return redirectedUrl;
} else {
return url;
}
}

Use urlfetchapp with own redirection?

I am trying to do an urlfetchapp on a website, the website contains HTML but thats good enough for me.
For example I want this url.
The problem of this method is it won't show me any data until I first went to this url.
I tried to do 2 urlfetchapp after each other. But this didn't work.
How can I solve this problem?
Below is my test code, if there is data then objTable should get data.
function loadXML()
{
try
{
var URL = "http://www.knzb.nl/verenigingen/wedstrijdsport/waterpolo/uitslagen_waterpolo/iframe/verenigingen/KNZB-DISTRICT-OOST/";
var response = UrlFetchApp.fetch(URL);
URL = "http://www.knzb.nl/verenigingen/wedstrijdsport/waterpolo/uitslagen_waterpolo/iframe/verenigingen/poule/1178/2074/DHS289F/";
response = UrlFetchApp.fetch(URL).getContentText();
Logger.log(response);
var doc = Xml.parse(response, true);
var objTable = doc.html.body.form.div[1].div.div.div.div.div.div;
}
catch(err)
{
var objTable = "Load failed";
Logger.log("Loading data failed: " + err);
}
return objTable;
}
I already found the answer to my own question.
Needed to resend the cookie. The script below is working as it should be.
function loadXML()
{
try
{
var URL = "http://www.knzb.nl/verenigingen/wedstrijdsport/waterpolo/uitslagen_waterpolo/iframe/verenigingen/KNZB-DISTRICT-OOST/";
var response1 = UrlFetchApp.fetch(URL);
Logger.log(response1);
var cookieStr = response1.getHeaders()["Set-Cookie"];
var params = {method: "POST", headers:{"Cookie": cookieStr}};
URL = "http://www.knzb.nl/verenigingen/wedstrijdsport/waterpolo/uitslagen_waterpolo/iframe/verenigingen/poule/1178/2074/DHS289F/";
response = UrlFetchApp.fetch(URL, params);
Logger.log(response.getAllHeaders());
response = response.getContentText();
var doc = Xml.parse(response, true);
var objTable = doc.html.body.form.div[1].div.div.div.div.div.div;
}
catch(err)
{
var objTable = "Load failed";
Logger.log("Loading data failed: " + err);
}
return objTable;
}

UrlFetchApp.fetch windows-1251

I wanted to get the title page of the head unit
I expect to get
Страховые компании Уфы. Адреса отделений в Уфе
And get
��������� �������� ���. ������ ��������� � ���
sample script:
function doGet() {
var options1 = {
contentType: "text/html; charset=windows-1251",
muteHttpExceptions: true
};
var url_catalog = 'http://ufa.insure-company.ru/';
var response = UrlFetchApp.fetch(url_catalog, options1);
var txt = response.getContentText();
doc = Xml.parse(txt, true);
var tbody = doc.html.head;
var title = tbody.getElements("title");
var t = title[0].getText();
return HtmlService.createHtmlOutput(t);
}
function doGet() {
var options1 = {
contentType: "text/html ",
muteHttpExceptions: true
};
var url_catalog = 'http://ufa.insure-company.ru/';
var response = UrlFetchApp.fetch(url_catalog, options1).getContentText("windows-1251");
doc = Xml.parse(response, true);
var tbody = doc.html.head;
var title = tbody.getElements("title");
var t = title[0].getText();
return HtmlService.createHtmlOutput(t);
}
It actually was answered in the old product forum see the answer here
You get the context in the UrlFetchApp class with getContentText().
Working example