4.2 Facebook Graph API

要使用FB Graph API, 要先取得自己的token, 可以到這個網站去申請: https://developers.facebook.com/tools/explorer

點選"取得token" -> "取得用戶存取token" -> 勾選你想讓這個token可以取得的資訊 -> 得到token

把這個token記著, 等等程式裡面要用(ACCESS_TOKEN).

import requests


# To obtain the access token, go to https://developers.facebook.com/tools/explorer.
ACCESS_TOKEN = ''


def get_my_friends():
    url = 'https://graph.facebook.com/v2.9/me?fields=id,name,friends&access_token={}'.format(ACCESS_TOKEN)
    data = requests.get(url).json()
    print('My ID: ' + data['id'])
    print('My name: ' + data['name'])
    print('Total friends: ', data['friends']['summary']['total_count'], 'friends.')


def get_page_post(page_id):
    url = 'https://graph.facebook.com/v2.9/{0}/posts?access_token={1}'.format(page_id, ACCESS_TOKEN)
    data = requests.get(url).json()
    print('There are ', len(data['data']), ' posts on the fans page.')
    print('The latest post time is: ', data['data'][0]['created_time'])
    print('Content:', data['data'][0]['message'])


def main():
    get_my_friends()
    get_page_post(1707015819625206)


if __name__ == '__main__':
    main()

輸出結果:

原始碼點我

Last updated

Was this helpful?