2014-09-05 71 views
1

我只是搞亂了Facebook的python API。Facebook Python API朋友分頁下一頁返回空數組

我想列出所有的朋友。

下面的代碼:

friends = graph.get_connections("me","friends") 

while(friends['data']): 
    for friend in friends['data']: 
     allfriends.append(friend['name']) 
    try: 
     friends=requests.get(friends['paging']['next']).json() 
    except KeyError: 
     print "Key Error" 

print allfriends 

此代碼只是列出了幾個朋友。當我嘗試向[分頁] [下一頁]中的url發出請求時,它只是返回一個空數組。

我無法理解,至於我哪裏錯了?下一個url應該從我的friendlist中檢索下一組名稱。

請幫忙。

謝謝

+1

如果有更多的朋友,那麼它應該返回更多的是。我的猜測是,您使用的是API v2.0或更高版本,這意味着您只會獲得已授予應用程序user_friends權限的朋友,而不是您所有的朋友 – WizKid 2014-09-05 12:25:19

+0

因此,其他朋友必須授予權限,我只需列出它們即可? – psyc0der 2014-09-05 12:29:44

+0

是的,這是正確的。我建議你閱讀https://developers.facebook.com/docs/apps/changelog – WizKid 2014-09-05 12:30:56

回答

0

你可能想嘗試以下操作:

friends = graph.get_connections("me","friends") 

while(friends['data']): 
    try: 
     for friend in friends['data']: 
      allfriends.append(friend['name']) 
     friends=requests.get(friends['paging']['next']).json() 
    except KeyError: 
     print "Key Error" 
print allfriends 

這是通過Facebook圖形得到正確的數據的一種更好的方式。

+0

'print'Key Error「'不會停止while循環。使用'break'語句會有所幫助 – 2016-03-24 03:12:06

0
https://graph.facebook.com/v2.2/me/friends 

上面的網址將只只返回誰使用(通過Facebook登錄)的應用程序提出請求的任何朋友。 Source