0

app.js

const app = express()
const port = process.env.PORT || 5000

var cors = require('cors')


var bodyparser= require('body-parser')

app.use(bodyparser.json());

const route=require('./routes')

app.use(cors());

app.use('/api',route)


app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})


routes.js

const express=require('express');
const Instagram = require('instagram-downloader');

const router=express.Router();

router.get('/',(req,res)=>{

    res.json("App Running...")

})


  
  
router.get("/inst", async (req,res)=>{
  
  
  ps=req.query.ps
    
    try {
      
    await Instagram(ps)
    .then(data => {
      const { entry_data: { PostPage } } = data;
      return PostPage.map(post => post.graphql.shortcode_media)
    })
    .then(images => images.map(img => res.json(img.video_url)))
    .then(console.log)
  
    } catch (error) {
      res.json(error)
    }
  
  })
  
  
  
  
module.exports=router;

package.json

{
  "name": "final",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node apps.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "instagram-downloader": "0.0.0"
  }
}

localhost result

when i run API call http://localhost:5000/api/inst?ps=https://www.instagram.com/p/BaaDHe5AdPH/?utm_source=ig_web_copy_link it returns me a JSON.

heroku result when i run same API call (https://reels2.herokuapp.com/api/inst?ps=https://www.instagram.com/p/BaaDHe5AdPH/?utm_source=ig_web_copy_link) on heroku it returns me nothing.

Please help me how can i fix this problem

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Abubakar
  • 43
  • 5

2 Answers2

2

It's most probably due to IP address from where the request is made. Instagram won't allow such API requests. It's working on your localhost because Instagram can detect that this IP belongs to a real person not a virtual server.

It can be solved by creating a session after authentication with username & password then using this session to make the API requests. Also note that logging in through the server will also cause an issue as the IP address won't be recognized and considered as suspicious account activity. You will be asked to pass a login challenge.

marty
  • 106
  • 2
  • 5
0

By dealing with errors correctly you will see the actual error.

its just the sad fact that error object are serialized to {} by default I feel like nodejs creator did a bad choice here. nevertheless :

var error = new Error('hello');
res.json(error)
// returns an empty object

because

var error = new Error('hello');
console.log(JSON.stringify(error));
// shows {}
console.log(error);
// shows actual error
console.log(JSON.stringify({message: error.message, stack: error.stack}));
// "correctly" convert error to json

Another way to serialize the error object is using this package : https://www.npmjs.com/package/serialize-error

and a detailed explanation of the situation : Is it not possible to stringify an Error using JSON.stringify?

Raphael PICCOLO
  • 2,095
  • 1
  • 12
  • 18