如何从facebook javascript SDK获取个人资料图片?

如何从facebook javascript SDK获取个人资料图片?,javascript,ajax,facebook,Javascript,Ajax,Facebook,我正在尝试从facebook获取个人资料图片。现在我正在从facebook获取所有信息,但无法获取用户的个人资料图片。这是我的密码: function getFBData () { FB.api('/me', function(response) { fbinfo = new Array(); fbinfo[0] = response.id; fbinfo[1] = response.first_name; fbinfo[2] = resp

我正在尝试从facebook获取个人资料图片。现在我正在从facebook获取所有信息,但无法获取用户的个人资料图片。这是我的密码:

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);
        }); 
如何从这个API的响应中获得图片。

试试看

FB.api("/me", {fields: "id,name,picture"}, function(response)
{

    FB.api(
            {
                method: 'fql.query',
                query: 'SELECT pid, src_big, src_big_height, src_big_width FROM photo WHERE aid IN ( SELECT aid FROM album WHERE owner="' + response.id + '" AND name = "Profile Pictures")'
            },
            function(data1) {
                alert( data1[0].src_big );
            }
    );

});
这将为您提供图像的链接,您可以适当地使用它


为什么不直接使用已检索到的id并显示图像?你为什么要多打一个电话

e、 g

例如,重定向到马克·祖克的照片

如果您在注销url以确保返回有效id时遇到问题,请将url粘贴到浏览器中。

尝试以下操作:

FB.api('/me/picture?type=normal', function(response) {
    var str="<img style='border: 3px solid #3a3a3a;' src='"+response.data.url+"'/>";
    return str;
});
FB.api('/me/picture?type=normal',函数(响应){
var str=“”;
返回str;
});

不应该是response.data.url…尝试使用response.picture.data.url

您可以尝试以下示例:

function(){
  var url=null;
    FB.getLoginStatus(function(response){

  if (response.status === 'connected') {
    console.log("getting photo")
    FB.api(
    "/me/picture",
    function (response) {
      if (response && !response.error) {
      console.log(response);
      url=response.data.url;
      console.log(url);
    }else {
      console.log("Error Occured");
    }
    }
);
  }
});
  return url;
};

我已经试过了,现在运行成功了。试试这个:

FB.api('/me', { fields: 'id, name, email' }, function(response) 
{
    var userInfo = document.getElementById('user-info');
    userInfo.innerHTML = '<img src="https://graph.facebook.com/' + response.id + '/picture">' + response.name ;

});
FB.api('/me',{fields:'id,name,email'},函数(响应)
{
var userInfo=document.getElementById('user-info');
userInfo.innerHTML=''+response.name;
});

希望你能得到答案。

我知道这篇文章很旧,但其他答案给了我的网站断开的链接(将用户ID与URL的其余部分连接起来以获得图像)

我发现正确的方法如下(根据这里的官方文件:):


从官方文档中,您获取用户的个人资料图片的方式是正确的,但代码缺失。来了

FB.api('/me', {fields: 'id,name,email,picture'}, function (response) {
    var picture_url = picture.data.url;
});
函数getFbUserData(){ api('/me',{locale:'en_US',字段:'id,first_name,last_name,email,link,gender,locale,picture'}, 功能(响应){ document.getElementById('profilepic')。innerHTML=''; }); }
您可以按照此链接中的说明操作>

默认情况下,当您尝试从配置文件获取响应时,您将只获得
id
name

FB.api('/me', function(response) {
  console.log('Successful login for: ' + response);
});

但是如果您想获得
电子邮件
图片
,则需要添加如下参数

FB.api('/me' {fields: "id, name, email, picture"}, function(response) {
  console.log('Successful login for: ' + response);
});


您是否通过FB.login()中的作用域请求了
user\u photos
权限?在尝试将其插入DOM之前,请再次检查
response.data.url
是否确实包含某些内容。配置文件pic不需要user\u photos权限,仅当您想访问所有个人资料图片和所有相册时才可使用。@LUKE Peterson,我只想获取当前用户个人资料图片。@LUKE Peterson我现在已经调用了FB.login函数,我只需要个人资料图片。这并不能回答问题。若要评论或要求作者澄清,请在其帖子下方留下评论。-@MikeT他们并没有要求澄清,他们是在赞扬hiraa,他说了什么要改变,但没有提供代码。不管怎样,你想让我改变我的答案吗?太好了!作品谢谢但是这幅画太小了,我怎么才能把它放大呢?再次感谢@DevRenanGaspar现在已经晚了,但是对于更大的配置文件图片,您可以使用FB.api('/me',{fields:'id,name,email,picture.width(500).height(500)},function(response){var picture_url=response.picture.data.url;});你的链接重定向到不需要的广告页面。这是一篇非常旧的文章,可能已经被删除或更改了。
function getFbUserData(){
FB.api('/me', {locale: 'en_US', fields: 'id,first_name,last_name,email,link,gender,locale,picture'},
function (response) {
    document.getElementById('profilepic').innerHTML = '<img src="'+response.picture.data.url+'"/>';

}); }
FB.api('/me', function(response) {
  console.log('Successful login for: ' + response);
});

FB.api('/me' {fields: "id, name, email, picture"}, function(response) {
  console.log('Successful login for: ' + response);
});