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
Related
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);
}
I can get account details so my authentication appears correct but in trying to modify that code to create an order it returns a code 401 "msg":"Invalid KC-API-SIGN". The modification involved adding in the method and payload and changing endpoint (/api/vi/accounts) to endpoint2 (/api/v1/orders)
function kucoinTest5()
{
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("xxxxx");
var key = sheet.getRange("xx").getValue()
var secret = sheet.getRange("xx").getValue();
var passphrase = sheet.getRange("xx").getValue();
var host = 'https://openapi-sandbox.kucoin.com';
//var endpoint ='/api/v1/accounts';
var endpoint2 ='/api/v1/orders';
var timestamp = ''+ new Date().getTime();
var strForSign = timestamp + 'GET' + endpoint2;
var signature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_256, strForSign, secret);
var encodedPass = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_256, passphrase, secret);
var url= host + endpoint2
var requestOptions = {
'method': "POST",
'headers': {
'KC-API-KEY': key,
'KC-API-TIMESTAMP': timestamp,
'KC-API-SIGN': Utilities.base64Encode(signature),
'KC-API-KEY-VERSION': '2',
'KC-API-PASSPHRASE': Utilities.base64Encode(encodedPass),
},
'payload': {
'clientOid': 'test1',
'side': 'buy',
'symbol': 'BTC-USDT',
'type': 'market',
'tradeType': 'TRADE',
'funds': 100
},
muteHTTPExceptions: true,
};
var httpRequest= UrlFetchApp.fetch(url, requestOptions);
//var getContext= httpRequest.getContentText();
Logger.log(httpRequest);
}
Solved above problem here is the code to post a buy order on KuCoin:
function kuCoinTest5()
{
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("xxxx");
var key = sheet.getRange("xx").getValue()
var secret = sheet.getRange("xx").getValue();
var passphrase = sheet.getRange("xx").getValue();
var payload = {
'clientOid':"UUID",
'side':"buy",
'symbol':"BTC-USDT",
'type':"market",
'tradeType':"TRADE",
'funds':"100"
};
var data = JSON.stringify(payload);
//Logger.log(data);
var host = 'https://openapi-sandbox.kucoin.com';
var timeStamp = ''+ new Date().getTime();
//var nowStr = "" + nowDate;
var endpoint ='/api/v1/accounts';
var endpoint2 ='/api/v1/orders';
var strForSign = timeStamp + "POST" + endpoint2 + data;
//Logger.log(strForSign);
var signature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_256, strForSign, secret);
var encodedPass = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_256, passphrase, secret);
var url= host + endpoint2;
//Logger.log(url);
var options = {
"method":"POST",
'headers' : {
'KC-API-KEY': key,
'KC-API-TIMESTAMP': timeStamp,
'KC-API-SIGN': Utilities.base64Encode(signature),
'KC-API-KEY-VERSION': '2',
'KC-API-PASSPHRASE': Utilities.base64Encode(encodedPass)
},
"contentType":"application/json",
"payload":data,
//'payload' : {'clientOid':"45234524625",
//'side':"buy",
//'symbol':"BTC-USDT",
//'type':"market",
//'tradeType':"TRADE",
//'funds':"100"},
"muteHttpExceptions":true,
}
var result = UrlFetchApp.getRequest(url, options);
Logger.log(result) // a better way to debug
var result = UrlFetchApp.fetch(url, options); // works perfectly in my case
Logger.log(result)
}
I had the same problem with a GET request, and finally solved thanks to the above code. Here is my code:
function KuCoinRequest(){
var key ='xx'
var secret = 'xx'
var passphrase = 'xx'
var url = "https://api-futures.kucoin.com/"; //endpoint
var timestamp = '' + Number(new Date().getTime()).toFixed(0);
var command = "GET";
var endpoint = "api/v1/fills"
var str_to_sign = timestamp + command +"/" + endpoint;
var signature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_256, str_to_sign, secret)
var encodedPass = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_256, passphrase, secret);
var params = {
'method': "GET",
'headers' : {
'KC-API-SIGN': Utilities.base64Encode(signature),
'KC-API-KEY': key,
'KC-API-TIMESTAMP': timestamp,
'KC-API-PASSPHRASE': Utilities.base64Encode(encodedPass),
'KC-API-KEY-VERSION': '2',
'muteHttpExceptions': true
}
};
query = url + endpoint;
var data = UrlFetchApp.fetch(query, params);
Logger.log(data.getContentText());
printJsonKucoin(data, endpoint);
return data;
}
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();
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<mpl=default<mplcache=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;
}
}
I get the error "No Preview Available" when I try to open it. I am trying to get an image from an external website.
Here are my code attempts:
var contentType = "image/jpeg";
var options = {
"method" : "get",
"contentType": contentType,
'muteHttpExceptions': true
};
var url = "www.externalwebsite.com/12344.jpg";
var response = UrlFetchApp.fetch(url, options);
Logger.log(response.getContentText());
var blob = response.getBlob();
blob.setContentTypeFromExtension();
Logger.log(blob.getContentType());
var image = blob.getAs(contentType);
var file = DriveApp.createFile("file1.jpg",blob,contentType);
var file2 = DriveApp.createFile(Utilities.newBlob(blob, MimeType.JPEG).getAs(MimeType.JPEG).setName('file2.jpg'));
var file3 = DriveApp.createFile(blob);
var data = Utilities.base64Decode(blob, Utilities.Charset.UTF_8);
var blobFile = Utilities.newBlob(data, MimeType.JPEG, "file4.jpg");
DriveApp.createFile(blobFile);
Also tried using Advanced Drive :
var file5 = {
title: 'file5.jpg',
mimeType: 'image/jpeg'
};
file = Drive.Files.insert(file5, blob);