Accessing Facebook API Page Insights with Python

假装没事ソ 提交于 2019-12-23 05:43:14

问题


I'm currently an administrator of a Facebook Page and I'm attempting to access some insights from it and I'm having lots of trouble gaining any access to the API. At the moment, it currently returns no data. Can anyone figure out why? Trying to get a code written that'll let me input whatever metrics I am searching for under the 'metric' part of the parameters.

graph=facebook.GraphAPI(access_token=token)
page_info=graph.get_object('me') # returns metadata for page
page_ID=page_info['id'] # returns the page ID to be used in calling the URL

base='https://graph.facebook.com/v2.9'
node='/'+ page_ID + '/insights'
url=base+node
parameters={'period':'week','access_token':token,'metric':'page_impressions'}
object=requests.get(url,params=parameters).text.encode('utf-8')
data=json.loads(object)
print data    

This returns the following (I've taken out the access token where it would be):

{u'paging': {

u'next': u'https://graph.facebook.com/v2.9/1509164045966534/insights?access_token=(my_hidden_token)&metric=page_impressions&period=week&since=1495255384&until=1495514584',

u'previous': u'https://graph.facebook.com/v2.9/1509164045966534/insights?access_token=(my_hidden_token)&metric=page_impressions&period=week&since=1494736984&until=1494996184'

},

u'data': []}


回答1:


Try this code:

base='https://graph.facebook.com/v2.9'
node='/'+ page_ID + '/insights/page_impressions'
url=base+node
parameters={'period':'week','access_token':token}
object=requests.get(url,params=parameters).text.encode('utf-8')
data=json.loads(object)
print data

metric is not parameter :)



来源:https://stackoverflow.com/questions/44105005/accessing-facebook-api-page-insights-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!