vendredi 25 juin 2021

How to get instagram profile photos with javascript?

https://www.instagram.com/USERNAME/?__a=1

How can I get profile picture from here? Better to have pure javascript but after a few tries it didn't work. Maybe it's because of CORS

I try this but it gives user not found error.

function getPhoto(a) {
  
  // validation for instagram usernames
  let regex = new RegExp(/^(?!.*\.\.)(?!.*\.$)[^\W][\w.]{0,29}$/);
  let validation = regex.test(a);

  if(validation) {
  
    $.get("https://www.instagram.com/"+a+"/?__a=1")
    .done(function(data) { 

      // getting the url
      let photoURL = data["graphql"]["user"]["profile_pic_url_hd"];

      // update img element
      $("#photoReturn").attr("src",photoURL)
     
     })
    .fail(function() { 
      // code for 404 error 
      alert('Username was not found!')
    })
  
  } else {
  
    alert('The username is invalid!')
  }

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<img src="" id="photoReturn">

<br><br>

<input type="text" id="usernameInput">

<button onclick="getPhoto($('#usernameInput').val().trim())">Get profile photo</button>



Aucun commentaire:

Enregistrer un commentaire