2014-04-19 81 views
2

我試圖通過fb.api()調用獲取所有Facebook評論者的個人資料圖片。目前所有評論得到outputed我只是不能得到個人資料圖片追加適當的評論。FB api呼叫循環

在for循環中循環遍歷所有註釋是另一個FB.api調用,它獲取對視頻進行註釋並將其追加到單個註釋塊中的用戶的配置文件圖片。通過此代碼,我可以獲得所有評論每位評論的用戶的個人資料照片。 我在做什麼錯?先謝謝你!

var komentarji_length = response.comments.data.length; 

for (var i = 0; i < komentarji_length; i++) { 
    var user_id = response.comments.data[i].from.id; 

    FB.api("/" + user_id + "/picture", { 
     access_token: access_token 
    }, function (response) { 
     var profile_pic_link = response.data.url; 
     $(".comments_profile_pic" + i).append("<img src=" + comments_profile_pic_link + ">"); 

    }); 

    var ime = response.comments.data[i].from.name; 
    var message = response.comments.data[i].message; 

    $(".fb_comments").append('<div class="single_comment"><div class="comments_profile_pic' + i + '"></div><div class="name">' + name + '&nbsp says:</div><div class="single_message">' + message + '</div></div>'); 
} 

回答

2

問題1:comments_profile_pic_link沒有定義,變量是:profile_pic_link

但是問題也解決不了。當您撥打"/" + user_id + "/picture"時,它會自動重定向到圖片網址。因此,您可以直接使用圖像網址:https://graph.facebook.com/{user-id}/picture。不需要進行不必要的API調用。

Example


不過如果你希望得到正確的圖像URL(雖然不要求),可以使用redirect=0您的呼叫 -

FB.api("/" + user_id + "/picture?redirect=0", { 
    access_token: access_token 
    }, function (response) { 
    var profile_pic_link = response.data.url; 
    $(".comments_profile_pic" + i).append("<img src=" + comments_profile_pic_link + ">"); 
});