NodeJS + Express - getting Instagram JSON data from URL

I'm trying to display the JSON data form instagram's URL: https://www.instagram.com/apple/?__a=1.

If I put this in browser, it shows the JSON, but I want to be able to get it with express so I can attempt to capture any data I need to with that URL

const express = require("express");
const Instagram = require("node-instagram").default;
const keys = require("./config/keys");
const app = express();
const request = require("request");

app.get("/", (req, res) => {
  const url = "https://www.instagram.com/apple/?__a=1";
  request(url, (err, response, body) => {
    const json = JSON.parse(body);
    console.log(json); 
    res.json(request.json);
  });
});

app.listen(5000);

When I'm trying to deploy this in localhost, I'm getting an error:

SyntaxError: Unexpected end of JSON input

1 answer

  • answered 2018-11-08 05:39 lucascaro

    If you are logged in to instagram in your browser, you are getting authentication via cookies, and everything works fine, but if you are not logged in, the url https://www.instagram.com/apple/?__a=1 responds wit a 403 (access denied) error. This is the case for your back-end code since you are not sending cookies or authentication headers.

    You are probably getting an error but not checking for errors in your request callback (the err parameter).

    According to the instagram developer documentation, you need to register your app and authenticate before you can perform the request with OAuth credentials.

    Check request's documentation on OAuth signing for more info.