2016-05-30 117 views
0

我無法弄清楚如何從頁面獲取fan_count。 我總是得到這個錯誤python facebook-sdk fan_count問題

Traceback (most recent call last): 
    File "./facebook_api.py", line 37, in <module> 
facebook_graph.get_object('somepublicpage')['fan_count'] 
KeyError: 'fan_count' 

的對象僅包含ID /名稱,我無法弄清楚如何才能得到「fan_count」數據給予更多的權限。

這裏是我使用的代碼:

import facebook 
import urllib 
import urlparse 
import subprocess 
import warnings 


warnings.filterwarnings('ignore', category=DeprecationWarning) 


oauth_args = dict(client_id = FACEBOOK_APP_ID, 
       client_secret = FACEBOOK_APP_SECRET, 
       grant_type = 'client_credentials') 
oauth_curl_cmd = ['curl', 
       'https://graph.facebook.com/oauth/access_token?' +  urllib.urlencode(oauth_args)] 
oauth_response = subprocess.Popen(oauth_curl_cmd, 
           stdout = subprocess.PIPE, 
           stderr = subprocess.PIPE).communicate()[0] 

try: 
    oauth_access_token = urlparse.parse_qs(str(oauth_response))['access_token'][0] 
except KeyError: 
    print('Unable to grab an access token!') 
exit() 

print oauth_access_token 
facebook_graph = facebook.GraphAPI(oauth_access_token) 
print facebook_graph.get_object(PROFILE_ID)['fan_count'] 

回答

1

由於圖形API的V2.4,你必須指定你想獲得返回的字段。這將是正確的API調用:

/{page-id}?fields=name,fan_count

它被稱爲 「聲明域」。

+0

謝謝!有用! –