Facebook API | Set-3 - GeeksforGeeks

25 Jun, 2019
Prerequisite: Facebook API | Set-1, Set-2
In this article we will be discussing three methods:
searchget_connectionsget_allconnections
search
get_connections
get_allconnections
The valid value types are place and placetopic.
Parameters:id: It is a string containing a valid value.args: This is optional and they need to passed a query params.
Example: Get details of all places that are near Connaught Place Delhi. The Latitude and Longitude of Connaught Place are – 28.6304, 77.2177
import jsonimport facebook  def main():    token = "Please replace with your access token"    graph = facebook.GraphAPI(token)      places = graph.search(type ='place', center ='28.6304, 77.2177',                                             fields ='name, location')      for place in places['data']:        print('%s %s' %(place['name'].encode(), place['location'].get('zip')))      if __name__ == '__main__':    main()
Please refer to the link Search Reference for complete list of fields that can used.
This methods aims at returning all connections or we can say Edges for the mentioned object as dict.
Parameters:id:A string specifying the unique id for the resource under question.connection name: A string specifying the connection or edges between the objects.
If connection name parameter is left empty then get_connections method will simply return the basic information of the authenticated user.
Example #1: We want to find out the total count of the number of Friends of the active user.
import jsonimport facebook  def main():    token = "Please replace this with your access token"    graph = facebook.GraphAPI(token)    friends = graph.get_connections(id ='me', connection_name ='friends')    print(json.dumps(friends, indent = 4))  if __name__ == '__main__':    main()
Example #2: We want to get list of all posts from a page. For this, we will be using get_connections and later on, we will demonstrate how to use get_all_connections method.
import jsonimport facebook  def main():    token = "Please replace this with your PAGE Access Token"    graph = facebook.GraphAPI(token)    posts_25 = graph.get_connections(id ='PAGE_ID', connection_name ='posts',                                                  fields ='id, created_time')      print(json.dumps(posts_25, indent = 4))  if __name__ == '__main__':    main() 
Note: This example by default print latest 25 posts. You can add a filter for the post using limit(...) to set the limit for the number of the posts.
Example #3: In this example we will use get_connections method to print all the comments for the posts in reverse_chronological order, show sub comments including hidden comments and also show the total count of the comments.
import jsonimport facebook  def main():    token = "Please replace this with your PAGE Access Token"    graph = facebook.GraphAPI(token)    posts_25 = graph.get_connections(id ='POST_ID', connection_name ='comments',                                      include_hidden = True, order ='reverse_chronological',                                     filter ='stream', summary ='total_count')      print(json.dumps(posts_25, indent = 4))  if __name__ == '__main__':    main() 
Iterates over all pages returned by a get_connections call and yields the individual items.
Parameters:id:A string specifying the unique id for the resource under question.connection name: A string specifying the connection or edges between the objects.
Example #1: In this example I have used get_all_connections to list all the posts from a Page listed using the since datetime parameter.
import jsonimport facebookfrom datetime import datetime   def main():    token = "Please replace this with your PAGE Access Token"    graph = facebook.GraphAPI(token)    posts_all = graph.get_all_connections(id ='PAGE_ID', connection_name ='posts',                                           fields ='created_time, id',                                           since = datetime(2017, 1, 1, 0, 0, 0))      for ind, post in enumerate(posts_all):     print(json.dumps(ind, indent = 4))     print(json.dumps(post, indent = 4))         if __name__ == '__main__':    main()
References:
https://developers.facebook.com/docs/marketing-api/insights/parametershttps://developers.facebook.com/docs/graph-api/
https://developers.facebook.com/docs/marketing-api/insights/parameters
https://developers.facebook.com/docs/graph-api/
Information-Security
GBlog
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
DSA Sheet by Love Babbar
GET and POST requests using Python
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Types of Software Testing
Working with csv files in Python
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
How to get column names in Pandas dataframe
Python Dictionary
Read a file line by line in Python