google maps tracks api - send request using node js - node.js

In order to access the google maps tracks API, i have access token sent by Google’s OAuth 2.0 Authorization Server. Now i need to send this access token with every http request i made to google map tracks API. Where should i put this access token in http request ?. I tried to put in headers as 'X-Access-Token' : 'myaccesstoken', but am getting response from api as below.
body: '{\n "error": {\n "errors": [\n {\n "domain": "global",\n "reason": "required",\n "message": "Login Required",\n "locationType": "header",\n "location": "Authorization"\n }\n ],\n "code": 401,\n "message": "Login Required"\n }\n}\n' }
I referred google map tracks API official documentation https://developers.google.com/maps/documentation/tracks/auth, but unable to solve this error.
below is the code am using to authenticate with Google’s OAuth 2.0 Authorization Server, and code to send request to google map tracks API.
var jwt = new googleapis.auth.JWT(client_email, keyFile, null, scopes, null);
jwt.authorize(function(jwtErr, tokens){
if(jwtErr){
return;
}
else{
headers = {
'content-type' : 'application/json',
'X-Access-Token' : tokens.access_token
};
options = {
url : 'https://www.googleapis.com/tracks/v1/geofences/list',
headers : headers,
method : 'POST',
};
request(options, function(error, response, body){
if(error){
return;
}
console.log(response);
});
}
});

Found the solution, access token should be put in
header : { 'Authorization' : 'myaccesstoken' },
so after changes, code will look like below.
var jwt = new googleapis.auth.JWT(client_email, keyFile, null, scopes, null);
jwt.authorize(function(jwtErr, tokens){
if(jwtErr){
return;
}
else{
headers = {
'content-type' : 'application/json;charset=utf-8',
'content-length': Buffer.byteLength(data),
**'Authorization' : tokens.access_token**
};
options = {
host: 'www.googleapis.com',
path: '/tracks/v1/entities/create',
headers : headers,
method : 'POST',
};
var post_req = https.request(options, function(res){
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response: ' + chunk);
});
});
post_req.on('error', function(error_msg){
console.log(error_msg);
});
post_req.write(data);
post_req.end();
}
});

Related

NodeJS + Request REST API

I have the following code an i try to connect to a server and get a json response from the server but somthing is wrong becouse i get this error: error null 401. Can you please help me to identify the error on this code or to find what is missing. Thank you!
var request = require('request');
var apiurl = "externalserverurl:6080";
var options = {
"regid" : "username",
"password" : "password",
"uri" : apiurl,
"registrar_domain" : "mydomain.com",
"lang" : "en",
json: true,
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'User-Agent': 'request'
}
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log('body:', body);
} else {
console.log('error', error, response && response.statusCode);
}
}
request(options, callback);
As #kay already mentioned, errorcode 401 means that your request is unauthorized. Given that there are a lot of different possible authentication methods (basic auth, digest auth, OAuth, you name it), you should refer to your target server and clarify which auth method you should use before trying to actually modify your code.
You could also check the response body, it could contain some hints about what exactly the server expects but doesn't receive.

Github Web API authorization issue - returns "Not found"

Since I don't find anyone with the same problem, I'm hoping it's a simple thing. And I'm fairly newb.
It's a Node/Express app and I'm trying to connect to Githubs Web API and retrieve issues for a certain repo. I have generated a personal token and I'm using it with Basic Authorization. When I try to connect I get: "Not found". Github state that where authentication is necessary they return 404/403, and I get 404, so it has to be something with the authentication. The repo has issues.
There shouldn't be a problem with the token, I choosed "repo" as access scope.
Actually I have no idea what I'm doing making HTTP requests but I've tried with both request and http. So among other things I'm wondering if it's something with the Authorization header that's wrong.
I paste the code I have right now using request, and the body returned. Any help is greatly appreciated.
const githubToken = process.env.TOKEN;
let options = {
url: "https://api.github.com/repos/myOrg/myRepo/issues/",
headers: {
"Authorization": "Basic " + githubToken,
"User-Agent": "Mozilla/5.0",
"Content-Type": "application/json"
}
};
let requestCB = function(error, response, body) {
if (error) {
console.log(error);
} else if (!error && response.statusCode == 200) {
let info = JSON.parse(body);
console.log("Success");
console.log(info);
} else {
console.log(response);
console.log(body);
}
};
request(options, requestCB);
And the end of the response and the body:
read: [Function],
body: '{"message":"Not Found","documentation_url":"https://developer.github.com/v3"}' }
{"message":"Not Found","documentation_url":"https://developer.github.com/v3"}
EDIT:
So I found help with this, posting solution below. I guess the problem was that the accept-header have to be included. Perhaps the hostname, path and uri have to be in this format aswell.
let options = {
headers: {
"User-Agent": "GitHub-username",
"Accept": "application/vnd.github.v3+json",
"Authorization": "token " + githubToken
},
hostname: "api.github.com",
path: "/repos/org/repo/issues",
uri: "https://api.github.com/repos/org/repo/issues",
method: "GET"
};
Basic authentication require no token
Basic Authentication
curl -u "username" https://api.github.com
So in your request, you can omit githubToken
See the docs for more information about types of authentication
Using a token
var request = require('request');
var options = {
url: 'https://api.github.com/?access_token=OAUTH-TOKEN',
headers: {
"Authorization": "token ",
"User-Agent": "Mozilla/5.0",
"Content-Type": "application/json"
}
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}
request(options, callback);

Setting Cookie in http response header from AWS lambda Node JS

I have a Lambda proxy integration enabled, and setting the response headers as part of Lambda output and API Gateway that will return them as part of the HTTP response to the client.
Sample code:
callback(null, {
"statusCode": 302,
"Location" : "https://somewebsite.com"
"headers": { "headerName": "headerValue", ... },
"body": "..."
});
I need to send out 3 cookies in the headers. I tried. But, failed:
callback(null, {
"statusCode": 302,
"Location" : "https://somewebsite.com"
"headers": { "Set-Cookie": [cookie1String, cookie2String, cookie3String] },
"body": "..."
});
[Edit]
I concatenated the cookie and passed in as the response, the client gets the cookie. But when the client calls the target in "location", the request does not have the cookie in the header.
callback(null, {
"statusCode": 302,
"Location" : "https://somewebsite.com"
"headers": { "Set-Cookie": c1=cookie1String;c2=cookie2String; c3=cookie3String] },
"body": "..."
});
Please help in sending these 3 cookies out to my client.
Use multiValueHeaders instead of headers.
const response = {
isBase64Encoded: true,
statusCode: 200,
multiValueHeaders : {"Set-Cookie": [`language=${language}`, `theme=${theme}`]},
body: JSON.stringify('User profile set successfully')
};
callback(null, response);
If you need it to be smarter, consider something like
function createHeaders(headers) {
const defaultHeaders = {
'Access-Control-Allow-Origin': '*',
};
const allHeaders = Object.assign({}, defaultHeaders, headers);
const singleValueHeaders = {};
const multiValueHeaders = {};
Object.entries(allHeaders).forEach(([key, value]) => {
const targetHeaders = Array.isArray(value) ? multiValueHeaders : singleValueHeaders;
Object.assign(targetHeaders, { [key]: value });
});
return {
headers: singleValueHeaders,
multiValueHeaders,
};
}
Then use it in the callback function.
callback(null, {
statusCode: status || 200,
body: JSON.stringify(body),
...createHeaders({ 'Set-Cookie': cookie }),
});
API gateway does not let you map the same header more than once. I got around by using different casing to set-cookie method.
callback(null, {
"statusCode": 302,
"Location" : "https://somewebsite.com"
"headers": { "Set-Cookie": cookie1, "set-Cookie": cookie2 },
"body": "..."
});
I would say that your issue is related to the fact that your response object in the callback is not formatted the way the api gateway expects.
These links reference aws documentation specifically to that.
http://docs.aws.amazon.com/apigateway/latest/developerguide/handle-errors-in-lambda-integration.html
Issue with your code...
'location' does not look like a valid property
Make sure your header key/value pairs are actual JSON objects using something like JSON.stringify
Don't forget to enable logs for both api gateway and lambda with full requests and responses. These two logs will help you debug.

Mailchimp API v3.0 add email to list via NodeJS http

I'm using NodeJS to call the new MailChimp 3.0 API in order to add an email to a list. While I can get it working via POSTman, I'm having a hard time with Node's http:
var http = require('http');
var subscriber = JSON.stringify({
"email_address": "test#test.com",
"status": "subscribed",
"merge_fields": {
"FNAME": "Tester",
"LNAME": "Testerson"
}
});
var options = {
host: 'https://us11.api.mailchimp.com',
path: '/3.0/lists/<myListID>/members',
method: 'POST',
headers: {
'Authorization': 'randomUser myApiKey',
'Content-Type': 'application/json',
'Content-Length': subscriber.length
}
}
var hreq = http.request(options, function (hres) {
console.log('STATUS CODE: ' + hres.statusCode);
console.log('HEADERS: ' + JSON.stringify(hres.headers));
hres.setEncoding('utf8');
hres.on('data', function (chunk) {
console.log('\n\n===========CHUNK===============')
console.log(chunk);
res.send(chunk);
});
hres.on('end', function(res) {
console.log('\n\n=========RESPONSE END===============');
});
hres.on('error', function (e) {
console.log('ERROR: ' + e.message);
});
});
hreq.write(subscriber);
hreq.end();
Rather than getting even some sort of JSON error from Mailchimp, however, I'm getting HTML:
400 Bad Request
400 Bad Request
nginx
Is it clear at all what I"m doing wrong here? It seems pretty simple, yet nothing I've tried seems to work.
A few additional thoughts:
While http's options have an "auth" property, I'm using the headers instead to ensure the authorization is sent without the encoding (as mentioned here). Still, I've also tried with the "auth" property, and I get the same result.
I'm actually making this call from inside an ExpressJS API (my client calls the Express API, that calls the above code - I've edited all that out of this example for simplicity). That's why my variables are "hres" and "hreq", to distinguish them from the "res" and "req" in Express. Is there any reason that could be the issue?
As mentioned above, I am able to get successful results when using POSTman, so I at least know my host, path, list ID, and API key are correct.
It turns out this had a very simple solution: the "host" property of the options object needed to have only the domain name. IE, remove the "https://" protocol:
var options = {
host: 'us11.api.mailchimp.com',
path: '/3.0/lists/<myListID>/members',
method: 'POST',
headers: {
'Authorization': 'randomUser myApiKey',
'Content-Type': 'application/json',
'Content-Length': subscriber.length
}
}
Try this , its working fine for Me.
var request = require('request');
function mailchimpAddListCall(email, cb){
var subscriber = JSON.stringify({
"email_address": email,
"status": "subscribed"
});
request({
method: 'POST',
url: 'https://us13.api.mailchimp.com/3.0/lists/<Your list id>/members',
body: subscriber,
headers:
{
Authorization: 'apikey <your Mailchimp API key>',
'Content-Type': 'application/json'
}
},
function(error, response, body){
if(error) {
cb(err, null)
} else {
var bodyObj = JSON.parse(body);
console.log(bodyObj.status);
if(bodyObj.status === 400){
cb(bodyObj.detail, null);
}
var bodyObj = JSON.parse(body);
cb(null, bodyObj.email_address +" added to list.");
}
});
}
request is a node module, that you'll need to install into your package.json. npm install --save request
You can use the auth properties just fine with API v3, but if you're getting a 400, that's not the problem. The body of the 400 Error should provide more detailed information, but one thing that jumps out immediately: MailChimp doesn't allow fake or fake-looking emails to be added to lists (like test#test.com), so I'd try a real address and see if that works for you.

Google Maps Tracks API 401 Invalid Credentials

I do have checked validity of my access_token at https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=mytoken
which is replying as follows,
{
"issued_to": "myservice_account_emailid",
"audience": "myservice_account_emailid",
"scope": "https://www.googleapis.com/auth/tracks",
"expires_in": 1300,
"access_type": "offline"
}
which means still my token is valid, but if i make request to google tracks api from my application, using the same access_token, am getting response as below ,
Response: {
"error": {
"errors": [
{
"domain": "global",
"reason": "authError",
"message": "Invalid Credentials",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Invalid Credentials"
}
}
Even i created new project in google developer console, and created new service account , and used the new credentials in application, but still getting same response. what could be the reason?
Found my self.
My error prone code is as below ,
var jwt = new googleapis.auth.JWT(client_email, keyFile, null, scopes, null);
jwt.authorize(function(jwtErr, tokens){
if(jwtErr){
return;
}
else{
headers = {
'content-type' : 'application/json;charset=utf-8',
'content-length': Buffer.byteLength(data),
'Authorization' : tokens.access_token
};
options = {
host: 'www.googleapis.com',
path: '/tracks/v1/entities/create',
headers : headers,
method : 'POST',
};
var post_req = https.request(options, function(res){
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response: ' + chunk);
});
});
post_req.on('error', function(error_msg){
console.log(error_msg);
});
post_req.write(data);
post_req.end();
}
});
The problem is in header object, 'Authorization' field is set to only access.token value, along with that we must specify token_type value also.
so header must be like below.
headers = {
'content-type' : 'application/json;charset=utf-8',
'content-length': Buffer.byteLength(data),
'Authorization' : tokens.token_type+' '+tokens.access_token
};
and now it solves 401 Invalid Credentials error.

Resources