Python facebook API get friends list
API Reference¶This page contains specific information on the SDKs classes, methods and functions. class facebook.GraphAPI¶A client for the Facebook Graph API. The Graph API is made up of the objects or nodes in Facebook (e.g., people, pages, events, photos) and the connections or edges between them (e.g., friends, photo tags, and event RSVPs). This client provides access to those primitive types in a generic way. You can read more about Facebooks Graph API here. Parameters
Example import facebook
graph = facebook.GraphAPI(access_token="your_token", version="2.12")
Methods¶get_object¶Returns the given object from the graph as a dict. A list of supported objects can be found here. Parameters
Examples # Get the message from a post.
post = graph.get_object(id='post_id', fields='message')
print(post['message'])
# Retrieve the number of people who say that they are attending or
# declining to attend a specific event.
event = graph.get_object(id='event_id',
fields='attending_count,declined_count')
print(event['attending_count'])
print(event['declined_count'])
# Retrieve information about a website or page:
# https://developers.facebook.com/docs/graph-api/reference/url/
# Note that URLs need to be properly encoded with the "quote" function
# of urllib (Python 2) or urllib.parse (Python 3).
site_info = graph.get_object(id="https%3A//mobolic.com",
fields="og_object")
print(site_info["og_object"]["description"])
get_objects¶Returns all of the given objects from the graph as a dict. Each given ID maps to an object. Parameters
Examples # Get the time two different posts were created.
post_ids = ['post_id_1', 'post_id_2']
posts = graph.get_objects(ids=post_ids, fields="created_time")
for post in posts:
print(post['created_time'])
# Get the number of people attending or who have declined to attend
# two different events.
event_ids = ['event_id_1', 'event_id_2']
events = graph.get_objects(ids=event_ids, fields='attending_count,declined_count')
for event in events:
print(event['declined_count'])
search¶https://developers.facebook.com/docs/places/search Valid types are: place, placetopic Parameters
Example # Search for places near 1 Hacker Way in Menlo Park, California.
places = graph.search(type='place',
center='37.4845306,-122.1498183',
fields='name,location')
# Each given id maps to an object the contains the requested fields.
for place in places['data']:
print('%s %s' % (place['name'].encode(),place['location'].get('zip')))
get_connections¶Returns all connections for a given object as a dict. Parameters
Examples # Get the active user's friends.
friends = graph.get_connections(id='me', connection_name='friends')
# Get the comments from a post.
comments = graph.get_connections(id='post_id', connection_name='comments')
get_all_connections¶Iterates over all pages returned by a get_connections call and yields the individual items. Parameters
put_object¶Writes the given object to the graph, connected to the given parent. Parameters
Examples # Write 'Hello, world' to the active user's wall.
graph.put_object(parent_object='me', connection_name='feed',
message='Hello, world')
# Add a link and write a message about it.
graph.put_object(
parent_object="me",
connection_name="feed",
message="This is a great website. Everyone should visit it.",
link="https://www.facebook.com")
# Write a comment on a post.
graph.put_object(parent_object='post_id', connection_name='comments',
message='First!')
put_comment¶Writes the given message as a comment on an object. Parameters
Example graph.put_comment(object_id='post_id', message='Great post...')
put_like¶Writes a like to the given object. Parameters
Example graph.put_like(object_id='comment_id')
put_photo¶https://developers.facebook.com/docs/graph-api/reference/user/photos#publish Upload an image using multipart/form-data. Returns JSON with the IDs of the photo and its post. Parameters
Examples # Upload an image with a caption.
graph.put_photo(image=open('img.jpg', 'rb'),
message='Look at this cool photo!')
# Upload a photo to an album.
graph.put_photo(image=open("img.jpg", 'rb'),
album_path=album_id + "/photos")
# Upload a profile photo for a Page.
graph.put_photo(image=open("img.jpg", 'rb'),
album_path=page_id + "/picture")
delete_object¶Deletes the object with the given ID from the graph. Parameters
Example graph.delete_object(id='post_id')
get_permissions¶https://developers.facebook.com/docs/graph-api/reference/user/permissions/ Returns the permissions granted to the app by the user with the given ID as a set. Parameters
Example # Figure out whether the specified user has granted us the
# "public_profile" permission.
permissions = graph.get_permissions(user_id=12345)
print('public_profile' in permissions)
get_auth_url¶https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow Returns a Facebook login URL used to request an access token and permissions. Parameters
Example app_id = "1231241241"
canvas_url = "https://domain.com/that-handles-auth-response/"
perms = ["manage_pages","publish_pages"]
fb_login_url = graph.get_auth_url(app_id, canvas_url, perms)
print(fb_login_url)
|