简体   繁体   中英

Unexpected end of JSON input getting Instagram JSON data with request

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM