The Code:
(URL is a working rest api that passes json data)
var request = require('request');
var username = "user";
var password = "pass";
var auth = "Basic " + new Buffer(username + ":" + password).toString("base64");
var url = "URL";
request(
{
method: "GET",
url : url
},
function (error, response, data) {
console.log(data);
var initial_index = Object.keys(data.sites)[0];
var product_index = Object.keys(data.sites[initial_index].products)[0];
var order_id = data.purchase_id;
var title = data.sites[initial_index].products[product_index].title;
var content = data.sites[initial_index].products[product_index].description;
var image = data.sites[initial_index].products[product_index].image;
var total_price = data.sites[initial_index].prices.final_price;
var quantity = data.sites[initial_index].products[product_index].input_fields.quantity;
var sold_by = data.sites[initial_index].info.name;
var order_status = data.sites[initial_index].status;
var datatwo = {
"status": "published",
"order_id": order_id,
"title": title,
"content": content,
"image": image,
"final_price": total_price,
"quantity": quantity,
"sold_by": sold_by,
"order_status": order_status
};
}
);
I receive this error when running the code. How can it be resolved?
var initial_index = Object.keys(data.sites)[0];
^
TypeError: Cannot convert undefined or null to object
You're not parsing the JSON (which is text) you get back. Add this at the top of your request callback:
data = JSON.parse(data);
E.g.:
request(
{
method: "GET",
url : url
},
function (error, response, data) {
data = JSON.parse(data);
var initial_index = Object.keys(data.sites)[0];
// ...
One you've parsed it, you'll have an object tree you can traverse.
Related
I have been trying to get an api call to Amazon SES to work for sending bulk emails. After figuring out issues with dates I'm now getting an error about the signatures mismatching. My code is below
function sendEmails() {
var template = HtmlService.createHtmlOutputFromFile('EmailTemplate.html');
var output = template.getContent();
var subject = 'Message from Mr. Bunz';
var data = JSON.stringify(
{
"Content": {
"Simple": {
"Body": {
"Html": {
"Data": output
},
},
"Subject": {
"Data": subject
}
},
},
"Destination": {
"ToAddresses": [ "example#gmail.com" ]
},
"FromEmailAddress": "no-reply#mail.example.com",
"ReplyToAddresses": [ "josh#example.com" ]
}
);
var url = 'https://email.us-west-1.amazonaws.com/v2/email/outbound-emails';
var aws_key_id = 'AKIAWLG4NO6GFEXAMPLE';
var aws_key = 'wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY';
var today = new Date();
var amz_date_iso = Utilities.formatDate(today,"UTC","YYYYMMd'T'HHmmss'Z'");
var amz_date = Utilities.formatDate(today,"UTC",'YYYYMMd');
console.log(amz_date)
var signature = awsSignature(aws_key,amz_date_iso);
var headers = {
"contentType": "application/json",
'X-Amz-Date': amz_date_iso,
"Authorization": "AWS4-HMAC-SHA256 Credential="+aws_key_id+"/"+amz_date+"/us-west-1/ses/aws4_request, SignedHeaders=content-type;host;x-amz-date;, Signature="+signature
};
console.log(headers);
var options = {
"method": "POST",
"headers": headers,
"payload": data,
"muteHttpExceptions" : true,
};
apiresponse = UrlFetchApp.fetch(url,options);
console.log(apiresponse.getContentText());
}
function awsSignature(key,dateStamp) {
var regionName = 'us-west-1';
var serviceName = 'iam';
var kDate = Utilities.computeHmacSha256Signature(dateStamp, "AWS4" + key);
var kRegion = Utilities.computeHmacSha256Signature(
Utilities.newBlob(regionName).getBytes(),
kDate
);
var kService = Utilities.computeHmacSha256Signature(
Utilities.newBlob(serviceName).getBytes(),
kRegion
);
var kSigning = Utilities.computeHmacSha256Signature(
Utilities.newBlob("aws4_request").getBytes(),
kService
);
kSigning = kSigning
.map(function(e) {
return ("0" + (e < 0 ? e + 256 : e).toString(16)).slice(-2);
})
.join("");
Logger.log(kSigning);
return kSigning;
}
The exact error message is below
{"message":"The request signature we calculated does not match the
signature you provided. Check your AWS Secret Access Key and signing
method. Consult the service documentation for details."}
I've tried generating a new key/id pair with no luck. The two answers on this issue on stackoverflow I have found are here and here.
I was getting a byte array back and couldn't figure out how to convert it into the needed signature so my implementation of creating the signature is based on this post.
Would greatly appreciate any and all help.
Update
After checking my signature code with the example values provided here I noticed that at the bottom where it gives the example output it says that while those are hex representations the actual signature should be binary. Now I'm having trouble converting hex to binary in GAS
i'm very new to Javascript and i just want to login into website from NodeJS request. This website need information from the first time visited to login.
Here is my code.
var cheerio = require('cheerio');
var loginLink = 'link';
var loginJar = request.jar();
var ltValue = '';
request.get({url: loginLink, jar: loginJar}, function(err, httpResponse, html)
{
var dat = cheerio.load(html);
var arr = dat('input[name="lt"]');
ltValue = arr.attr('value');
arr = dat('input[name="execution"]');
executionValue = arr.attr('value');
/*Post body*/
var loginBody = 'username=' + usn + '&password=' + pwd + '<=' + ltValue + '&execution=' + executionValue
request.post({url: loginLink, jar: loginJar, method: 'post', json: true, body: loginBody, }}, function(err, res, b)
{
if (b.indexOf('errors') != -1)
console.log("Success");
else console.log("Fail");
});
});
I have write try it in C# and it work correctly but in my NodeJs code it always return fail. I have tried everytime but i couldn't do it. Please help me with this problem.
byte[] binData = Encoding.ASCII.GetBytes(loginBody)
string loginFile = "loginInfo.txt";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("link");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = binData.Length;
request.CookieContainer = cookieContainer;
using (Stream stream = request.GetRequestStream())
{
stream.Write(binData, 0, binData.Length);
}
WebResponse response = request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
File.WriteAllText(loginFile, reader.ReadToEnd());
}
string loginData = userID + " " + password;
File.WriteAllText("login.txt", loginData);
I'm now working on the api of Gatecoin and want to get my open order on Google Sheet. I'm trying this:
function GetOrder() {
var method = "GET"
var URL = "https://api.gatecoin.com/Trade/Orders"
var contentType = "application/json"
var d = new Date()
var now = d.getTime()
var msg = method + URL + contentType + now
var publicKey = :Public_key
var privateKey = :Private_key
var hash = Utilities.computeHmacSha256Signature(msg, privateKey, Utilities.Charset.US_ASCII);
var hashInBase64 = Utilities.base64Encode(hash);
var options ={
'contentType': 'application/json',
'method': method,
'API_PUBLIC_KEY': publicKey,
'API_REQUEST_SIGNATURE': hashInBase64,
'API_REQUEST_DATE': now
};
var rdata = UrlFetchApp.fetch("https://api.gatecoin.com/Trade/Orders", options)
}
But the response mentions I was not logged in. What am I doing wrong?
I believe your options should have headers defined in an object as such:
var options ={
“contentType”: “application/json”,
“method”: method,
“headers”: {
“API_PUBLIC_KEY”: publicKey,
“API_REQUEST_SIGNATURE”: hashInBase64,
“API_REQUEST_DATE”: now
}
};
var protocol = "https";
var method = "POST";
var host = "mws.amazonservices.com";
var uri = "/Products/2011-10-01";
var marketPlaceId = "ATVPDKIKX0DER";
function generateRequest(asin, action){
var today = new Date();
time = today.toISOString();
var parameters = {
// "ASINList.ASIN.1":asin,
"Query":asin,
"AWSAccessKeyId":AWSAccessKeyId,
"Action": action,
"MarketplaceId":marketPlaceId,
"SellerId": SellerId,
"SignatureMethod":"HmacSHA256",
"SignatureVersion":"2",
"Timestamp":time,
"Version":"2011-10-01"
};
parameters = $.param( parameters );
var messageToEncrypt = method+"\n"+host+"\n"+uri+"\n"+parameters;
var sig = CryptoJS.HmacSHA256(messageToEncrypt, SecretKey);
sig = sig.toString(CryptoJS.enc.Base64);
sig = encodeURIComponent(sig);
parameters = parameters+"&Signature="+sig;
var mwsRequest = protocol+"://"+host+uri+"?"+parameters;
return mwsRequest;
}
// var asaUrl = generateRequest('B01I94N9TC','GetMatchingProduct');
var asaUrl = generateRequest('B01I94N9TC','ListMatchingProducts');
$.ajax({
url:asaUrl,
method: "POST",
success: function(data){
console.log(data)
}
});
It gives an error
"Check your AWS Secret Access Key and signing method. Consult the service documentation for details"
but if you send to Get Matching Product is operating normally
I'm trying to parse JSON I recieved from an API call, but I keep running into the error "TypeError: Cannot read property "id" from undefined. (line 42, file "")" I'm relatively new to Apps Script. Any ideas on what's going on? I can get the payload back in JSON, but can't seem to parse it.
function getInfo() {
var url = "https://subdomain.chargify.com/subscriptions.json";
var username = "xxx"
var password = "x"
var headers = {
"Authorization": "Basic " + Utilities.base64Encode(username + ':' + password)
};
var options = {
"method": "GET",
"contentType": "application/json",
"headers": headers
};
var response = UrlFetchApp.fetch(url, options);
var data = JSON.parse(response.getContentText());
Logger.log(data);
var id = data.subscription; // kicks back an error
// var id = data; works and returns the whole JSON payload
var ss = SpreadsheetApp.getActiveSheet()
var targetCell = ss.setActiveSelection("A1");
targetCell.setValue(id);
}
According to the documentation here
https://docs.chargify.com/api-subscriptions#api-usage-json-subscriptions-list
it returns an array of subscriptions when you call the /subscriptions.json endpoint. So probably your data object should be handled like:
for (var i=0;i<data.length;i++) {
var item = data[i]; //subscription object, where item.subscription probably works
Logger.log(JSON.stringify(item));
}
function getInfo() {
var url = "https://subdomain.chargify.com/subscriptions.json";
var username = "xxx"
var password = "x"
var headers = {
"Authorization": "Basic " + Utilities.base64Encode(username + ':' + password)
};
var options = {
"method": "GET",
"contentType": "application/json",
"headers": headers
};
var response = UrlFetchApp.fetch(url, options);
var data = JSON.parse(response.getContentText());
for (var i = 0; i < data.length; i++) {
var item = data[i]; //subscription object, where item.subscription probably works
Logger.log(JSON.stringify(item));
var subscriptionid = item.subscription.id;
}
var ss = SpreadsheetApp.getActiveSheet()
var targetCell = ss.setActiveSelection("A2");
targetCell.setValue(subscriptionid);
}