2015-08-15 53 views
0

我使用Passport.js將我的用戶登錄到Facebook,然後我想獲取他們的朋友列表。我使用FbGraph來請求Facebook圖形API。使用Facebook圖形API在Node.js中檢索整個friendList

到目前爲止,這麼好。 Facebook圖形API結果已分頁,但我無法超越第一頁。然後回覆是空的。

下面的代碼:

var graph = require('fbgraph'); 

// accessToken is returned by Express 
graph.setAccessToken(accessToken); 

var getFriends = function(url) { 
    graph.get(url, function (err, res) { 
     console.log(res); 
     if (res.paging && res.paging.next) { 
      getFriends(res.paging.next); 
     } 
    }); 
}; 

getFriends('/me/friends'); 

和輸出:

{ data: 
    [ { name: 'Gonzague xxx', id: 'xxx' }, 
    { name: 'Cyprien xxx', id: 'xxx' }, 
    { name: 'Chloé xxx', id: 'xxx' }, 
    { name: 'Elena xxx', id: 'xxx' }, 
    { name: 'Sari xxx', id: 'xxx' }, 
    { name: 'Marie xxx', id: 'xxx' } ], 
    paging: { next: 'https://graph.facebook.com/v2.0/xxx/friends?access_token=xxx&limit=5000&offset=5000&__after_id=enc_xxx' }, 
    summary: { total_count: 424 } } 
    404 
    { data: [], 
    paging: { previous: 'https://graph.facebook.com/v2.0/xxx/friends?access_token=xxx' }, 
    summary: { total_count: 424 } 
} 

回答

2

不幸的是,你不能在圖形API 2.0+整個好友列表,你只能得到你的朋友誰正在使用您的應用程序(使用手段,已授予在Facebook上的應用程序的權限)。 total_count鍵意味着用戶有424個朋友,這並不意味着您可以從您的應用中獲得所有424個朋友。

您看到的列表只是授權您的應用的人員,由於該列表中只有少數人,所以您只能看到分頁結果爲空。

+0

真的嗎?我在PHP中有相同的實現,它返回了整個friendList。有沒有辦法在JS中這樣做(也許通過使用以前版本的API ...)? – Buzut

+1

@ user1717735它是什麼時候?如果是在強制Graph API 2.0遷移(2015年4月30日)之前,那麼很可能現在不幸沒有。您可以嘗試的是查看Graph API 2.3中引入的「all_mutual_friends」邊緣,它可以提供您正在尋找的內容,如果兩個應用程序用戶之間的共同朋友信息足夠。在這裏查看Graph API 2.3的新功能:https://developers.facebook.com/docs/apps/changelog –

+0

是我最後一次嘗試PHP實現是在四月之前。然後,對於測試應用程序,如何測試你是否沒有任何使用該應用程序的朋友(不會對我的朋友說:「嘿,登錄這個以便我可以做測試!」)? – Buzut

相關問題