I'm trying to fetch some recent media of a location from the Instagram JSON endpoint: https://www.instagram.com/explore/locations/214991417/?__a=1
. This requires the client to have a valid authenticated session but I'm struggling to figure how to do this.
This is my current attempt:
var request = require('request').defaults({jar: true});
var cheerio = require('cheerio');
var uri = "https://www.instagram.com/accounts/login/?force_classic_login";
request(uri, function(error, response, body){
if (!error && response.statusCode == 200) {
var $ = cheerio.load(body);
var csrf = $("#login-form > input").attr("value");
var cookie = response.headers['set-cookie'];
request.post({uri: uri,
form: {
"username": "myinstauser",
"password": "myinstapass"
},
headers:{
'User-Agent': "bob",
"Cookie": cookie,
"x-csrftoken": csrf
}
}, function(err, response, body){
console.log(body);
});
}
});
But the response is a page with the following message:
This page could not be loaded. If you have cookies disabled in your browser, or you are browsing in Private Mode, please try enabling cookies or turning off Private Mode, and then retrying your ac tion.
Not sure if there's an easier way, but somehow I was logged in before. Maybe I authenticated manually or another plugin I used when playing around did it but I'd like to find the best way of doing this and the semi-scrapey way I'm doing it above feels very hacky.
Using the actual API is out of the question because I need to apply for full access instead of Sandbox mode and my usage is against their policy.
via Jazcash
No comments:
Post a Comment