Comments (7)
Thank you @1alind that worked!
Here's an updated code that others might help others:
let igUsername = "";
let igPassword = "";
const { IgApiClient } = require("instagram-private-api");
const axios = require("axios"); // node
async function getLatestPrivateMessages(username, password) {
const ig = new IgApiClient();
// Login to Instagram
ig.state.generateDevice(username);
await ig.account.login(username, password);
// Get the current user's inbox
const inbox = ig.feed.directInbox();
const threads = await inbox.items();
// Iterate over the threads and fetch messages
for (const thread of threads) {
const { thread_id } = thread;
const threadFeed = ig.feed.directThread({ thread_id });
const messages = await threadFeed.items();
// Process the messages
for (const message of messages) {
console.log(message);
let h = {
accept: "*/*",
"accept-language": "en-US,en;q=0.9",
"sec-ch-ua": '" Not A;Brand";v="99", "Chromium";v="102"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": '"Linux"',
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-site",
"x-asbd-id": "198387",
"x-ig-app-id": "936619743392459",
"x-ig-www-claim":
"hmac.AR2vqJv-rMUJZ0y3MD6rTCGpFTZHRY8OD0gGoEPuHcCI9jtN",
cookie:
'INSTA COOKIE',
Referer: "https://www.instagram.com/",
"Referrer-Policy": "strict-origin-when-cross-origin",
};
const response = await axios.get(
"https://i.instagram.com/api/v1/direct_v2/threads/" +
thread_id +
"/get_items/?item_ids=%5B%22" +
message.item_id +
"%22%5D&original_message_client_contexts=%5B%22" +
message.client_context +
"%22%5D",
{
headers: h,
body: null,
method: "GET",
}
);
console.log(response.data.items);
if (response.data.items[0].item_type == "clip") {
console.log(response.data.items[0].clip.clip);
}
}
}
}
// Usage
getLatestPrivateMessages(igUsername, igPassword).catch(console.error);
from instagram-private-api.
i use this
`let h = JSON.parse(fs.readFileSync('./files/h' + uid + '.json', 'utf8'));
const response = await axios.get("https://i.instagram.com/api/v1/direct_v2/threads/" + m.message.thread_id + "/get_items/?item_ids=%5B%22" + m.message.item_id + "%22%5D&original_message_client_contexts=%5B%22" + m.message.client_context + "%22%5D", {
headers: h,
body: null,
method: "GET"
});
var link = response.data.items[0].clip.clip.video_versions[0].url;
`
the ( h ) is cookies file from a browser session that i get and add manually ti file since reels are available in browsers ig u can use that and it works fine for me.. just remember to save the session when u login in browser.should be something like this:
{ "accept": "*/*", "accept-language": "en-US,en;q=0.9", "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"102\"", "sec-ch-ua-mobile": "?0", "sec-ch-ua-platform": "\"Linux\"", "sec-fetch-dest": "empty", "sec-fetch-mode": "cors", "sec-fetch-site": "same-site", "x-asbd-id": "198387", "x-ig-app-id": "936619743392459", "x-ig-www-claim": "hmac.AR2vqJv-rMUJZ0y3MD6rTCGpFTZHRY8OD0gGoEPuHcCI9jtN", "cookie": "mid=something; ig_did=something; ig_nrcb=1; csrftoken=something; ds_user_id=something; sessionid=something; shbid=\"something\\something\\something\"; shbts=\"something\\something\\something:something\"; rur=\"RVA\\something\\something:something\"", "Referer": "https://www.instagram.com/", "Referrer-Policy": "strict-origin-when-cross-origin" }
hope that helps :)
from instagram-private-api.
The solution is no longer working, this issue should be reopened @continue47.
from instagram-private-api.
@diragb
It's still working for me. Can you share more info about what's not working, what kind of error message do you get, and so on?
Make sure to copy your cookie from instagram into the cookie header in the code (use the developer tools in the browser to go to the network tab and grab the cookie from a request).
from instagram-private-api.
How long will the cookie last before we need to update the code @continue47 - any ideas?
from instagram-private-api.
For future reference, I'm using this function:
const ig = new IgApiClient()
// Login user and do your stuff..
const getInstagramHeaders = async () => {
const response = await axios.get('https://www.instagram.com/apple/')
const html = response.data as string
const CSRFToken = html.split('csrf_token')[1].split('\\"')[2]
const IGAppID = html.split('X-IG-App-ID')[1].split(',')[0].replaceAll('"', '').replace(':', '')
return {
...response.headers,
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:104.0) 20100101 Firefox/103.0',
'Accept': '*/*',
'Accept-Language': 'en,en-US;q=0.3',
'X-Csrftoken': CSRFToken,
'X-IG-App-ID': IGAppID,
'X-ASBD-ID': '198337',
'X-IG-WWW-Claim': 'hmac.AR2vqJv-rMUJZ0y3MD6rTCGpFTZHRY8OD0gGoEPuHcCI9jtN',
'Origin': 'https://www.instagram.com',
'DNT': '1',
'Alt-Used': 'i.instagram.com',
'Connection': 'keep-alive',
'Referer': 'https://www.instagram.com/',
'Referrer-Policy': 'strict-origin-when-cross-origin',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="102"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Linux"',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-site',
'Sec-GPC': '1',
'Cookie': ig.state.cookieJar.getCookieString('https://www.instagram.com')
}
}
from instagram-private-api.
I don't know how much the cookie lasts, it doesn't need to last that long for my use case.
from instagram-private-api.
Related Issues (20)
- Logout on Smartphone
- Is it possible to remove archived stories?
- how to fetch chats HOT 1
- dead function HOT 1
- dead HOT 1
- Adding a link to an Instagram story using instagram-private-api HOT 2
- How to get reel url from a chat HOT 1
- continue fetching data from where it was left off
- `ig.feed.reelsMedia` retrieves stories instead of reels HOT 1
- Using with cookie HOT 2
- Download full copy of account information
- media info not returning entire response
- Please HELP! Error Publishing video HOT 1
- Get Reels Tab Vidoes HOT 5
- Asking for assistance: Is there a way to retrieve a list of unanswered comments on a post?
- Error while uploading the video HOT 1
- collaboration
- How to share post as a story HOT 4
- Original audio is not showing in reels HOT 1
- how to set userAgent ?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from instagram-private-api.