Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch profile picture from facebook javascript SDK?

I am trying to fetch profile picture from facebook. Right now I am getting all information from facebook but unable to get profile pic of the user. Here is my code:

function getFBData () {
    FB.api('/me', function(response) {
      fbinfo = new Array();
      fbinfo[0] = response.id;
      fbinfo[1] = response.first_name;
      fbinfo[2] = response.last_name;
      fbinfo[3] = response.email;
      FB.api('/me/picture?type=normal', function (response) {
         var im = document.getElementById("profileImage").setAttribute("src", response.data.url);
         alert(im);
        }); 

How can I get picture from response of this API.

like image 230
Raghav Singh Avatar asked Jul 30 '14 10:07

Raghav Singh


People also ask

How can I trace a picture on Facebook?

In the search bar, on the left, you'll see a little camera icon. When you touch it, a drop-down menu should say "Search by Image." Click that. You should see two choices: Paste the URL of the image or Upload.


1 Answers

Why don't you just use the id you've already retrieved and display the image directly? Why do you need to make an extra call?

e.g

function getFBData () {
FB.api('/me', function(response) {
  fbinfo = new Array();
  fbinfo[0] = response.id;
  fbinfo[1] = response.first_name;
  fbinfo[2] = response.last_name;
  fbinfo[3] = response.email;

     var im = document.getElementById("profileImage").setAttribute("src", "http://graph.facebook.com/" + response.id + "/picture?type=normal");
});
}

For example http://graph.facebook.com/4/picture?type=normal redirects to Mark Zuck's picture

If you're having trouble log the url out to make sure you're getting a valid id back and paste the url into a browser.

like image 85
TommyBs Avatar answered Oct 07 '22 01:10

TommyBs