I am running my node/express js application on localhost. I am making a 'GET' request to Instagram's api and keep getting this error:
XMLHttpRequest cannot load https://api.instagram.com/oauth/authorize/?client_id=******&redirect_uri=http://localhost:4000/feed&response_type=code.
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:4000' is therefore not allowed access.
I make the request in my server like this:
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Headers: x-requested-with");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.get('/',function(req,res){
res.redirect(redirected to feed route);
})
app.get('/feed',function(req,response) {
var code = req.query.code;
var url = "https://api.instagram.com/oauth/access_token";
var options = {
url: url,
method: "POST",
form: {
client_id : clientId,
client_secret : client_secret,
grant_type: 'authorization_code',
redirect_uri: redirect_uri,
code: code
},
json: true
}
request(options, function(err,res,body){
var instagram_response = body;
response.json({access_info:instagram_response});
})
})
Getting data from visiting '/' in my server route works fine. When I call the server '/' route in the client side (when Gallery.html loads) using jQuery like below it gives me the error. Below is the function that runs when gallery.html is loaded in the client side.
$(function(){
$.get("/", function( instagram_reponse, access_token) {
access_token = instagram_reponse.access_token;
})
})
Am I getting this error because my node server is running on localhost? What am I doing wrong?