Savan Patel
Savan Patel

Reputation: 189

How to get Instagram Following List using Instagram graph api

How to get the following list from the Instagram account using the access token I tried everything but not work. here some API link which I tried before but none of them work.

I tried this one https://www.instagram.com/urvish_._/?__a=1

also this one I tried but nothing can help me.

Upvotes: 0

Views: 2040

Answers (1)

Esebi
Esebi

Reputation: 11

You can get the following (or also follower) list using the code below. Steps:

  1. Make sure you're logged in on instagram.com
  2. Open the API link: https://www.instagram.com/urvish_._/?__a=1 (your target username here is urvish_._)
  3. Open the browser console: normally Ctrl+Shift+J on Windows/Linux or +Option+J on Mac
  4. Paste this code and press Enter:
const GRAPHQL_MAX_PER_PAGE = 50;
async function getList() {
  let pageLimit = 200; // from my testing
  
  let baseInfo = JSON.parse(document.getElementsByTagName('body')[0].innerText);
  let userId = baseInfo.graphql.user.id;

  let config = { user_edge: 'edge_follow', query_hash: 'd04b0a864b4b54837c0d870b0e77e076', total_count: baseInfo.graphql.user.edge_follow.count };
  // for followers instead of followings:
  // { user_edge: 'edge_followed_by', query_hash: 'c76146de99bb02f6415203be841dd25a', total_count: baseInfo.graphql.user.edge_followed_by.count }
  
  let after = null, hasNext = true, thisList = [];
  for (pageCount = 1; hasNext && (pageCount <= pageLimit); ++pageCount) {
    try {
      let response = await fetch(`https://www.instagram.com/graphql/query/?query_hash=${config.query_hash}&variables=` + encodeURIComponent(JSON.stringify({
        id: userId, include_reel: true, fetch_mutual: true, first: GRAPHQL_MAX_PER_PAGE, after: after
      })));
      if (!response.ok) {
        console.warn(`Failed at page number ${pageCount.toLocaleString()}. HTTP status ${response.status}: ${response.statusText}.`);
        break;
      }
      try {
        response = await response.json();
      } catch (error) {
        console.error(`You may need to verify your account. Stopping. Failed at page number ${pageCount.toLocaleString()}.`, error);
        break;
      }
      hasNext = response.data.user[config.user_edge].page_info.has_next_page
      after = response.data.user[config.user_edge].page_info.end_cursor
      thisList = thisList.concat(response.data.user[config.user_edge].edges.map(({ node }) => {
        return {
          id: node.id,
          username: node.username,
          full_name: node.full_name,
          profile_pic_url: node.profile_pic_url,
        };
      }));
    } catch (error) {
      console.warn(`Error at page number ${pageCount.toLocaleString()}:`, error);
    }
    console.log(`${thisList.length.toLocaleString()} of ${config.total_count.toLocaleString()} fetched so far`);
  }
  console.info(`${thisList.length.toLocaleString()} fetched.`);
  console.log(thisList);
}

getList()

Browser console showing a fetched list after code execution

In the code I've set the page limit to 200 so you can get up to 10,000 of your followings.


PS: For a way to visualise your lists and get more details, you can try Instagram Lists, a tool I made.

Upvotes: 1

Related Questions