In [ ]:
#!pip install -U requests
#!pip install -U facebook-sdk
取得 token https://developers.facebook.com/tools/explorer
權限 friends, likes
In [ ]:
# 設定 token
token = input()
In [ ]:
import facebook
# 取得 graph api
graph = facebook.GraphAPI(token, version="2.7")
graph
In [ ]:
# 取得自己的資訊
obj = graph.get_object('me')
obj
In [ ]:
obj['name']
In [ ]:
# 取得更多資訊
import json
graph.get_object('me', fields=json.dumps(['id','name','gender', 'favorite_teams']))
In [ ]:
# 也可以用 id 來存取
obj = graph.get_object('10201382289514027', fields=json.dumps(['id','name','gender', 'favorite_teams']))
obj
In [ ]:
team_id = obj['favorite_teams'][0]['id']
In [ ]:
graph.get_object(team_id)
有哪些欄位可以查呢?
In [ ]:
# 看 metadata
metadata = graph.get_object('me', metadata=1)
metadata
metadata 資料很多,要如何看內容?
In [ ]:
type(metadata)
In [ ]:
# 先看一下有哪些 key
metadata.keys()
id 和 name 看起來還是一樣,重要的只有 metadata
In [ ]:
type(metadata['metadata'])
In [ ]:
metadata['metadata'].keys()
In [ ]:
for x in metadata['metadata']['fields']:
print(x['name'])
In [ ]:
for x in metadata['metadata']['connections']:
print(x)
In [ ]:
friends = graph.get_connections('me', connection_name = 'friends')
friends
In [ ]:
import requests
In [ ]:
from pprint import pprint
In [ ]:
friends = graph.get_connections('me', connection_name = 'friends')
i = 0
while True:
pprint(friends)
for f in friends['data']:
print(i, f)
i+=1
if 'paging' in friends and 'next' in friends['paging']:
friends=requests.get(friends['paging']['next']).json()
else:
break
In [ ]:
# 另外一種方式
graph.get_object('me', fields=json.dumps(['friends.limit(1000)']))
In [ ]:
#參考
#%load q_favorite_athletes.py
#%load q_likes.py