Back to Top

How to Get JSON Data from Facebook Graph Api in apex

Updated 18 July 2015

Apex provides several built-in classes to work with HTTP services and create HTTP requests like GET, POST, PUT, and DELETE.

You can use these HTTP classes to integrate to REST-based services. They also allow you to integrate to SOAP-based web services as an alternate option to generating Apex code from a WSDL.

These classes expose the general HTTP request/response functionality:

  • Http Class. Use this class to initiate an HTTP request and response.
  • HttpRequest Class: Use this class to programmatically create HTTP requests like GET, POST, PUT, and DELETE.
  • HttpResponse Class: Use this class to handle the HTTP response returned by HTTP.

The HttpRequest and HttpResponse classes support the following elements:

  • HttpRequest:
    • HTTP request types such as GET, POST, PUT, DELETE, TRACE, CONNECT, HEAD, and OPTIONS.
    • Request headers if needed.
    • Read and connection timeouts.
    • Redirects if needed.
    • Content of the message body.
  • HttpResponse:
    • The HTTP status code.
    • Response headers if needed.
    • Content of the response body.

The following example shows an HTTP GET request made to the external server specified by the value of url for this  You have to make a HTTP Callout to the URL

Searching for an experienced
Salesforce Company ?
Find out More
Http http =new Http();
HttpRequest req =new HttpRequest();
req.setEndpoint('https://graph.facebook.com/webkul?access_token=CAAGIV8ndRDcBALpKhd5t5smOzFMv25HLpgqMqfZBhwZCJMbqPFW4aOZCXphud9tc7w9EEw0vBvRooZB7idLHLK3LkzS0pQP63pysUUZA7We5EV8IqZAsj7WwEhXf97C34aDKyaKW8lyvihyxl2duEpMjuHGs7LlsyPEZAwSJjykSpomgALOdvZBV');
req.setmethod('GET');
HttpResponse res = http.send(req);
String str = res.getbody();
System.debug(str);
Output
{
   "id": "285021323071",
   "about": "https://store.webkul.com . WebKul Store is the largest collection of open source plugins , we build best marketplace and openERP connectors in the world .",
   "awards": "Best E-commerce Marketplace \n\nBest E-commerce OpenERP connector",
   "can_post": true,
   "category": "Software",
   "checkins": 0,
   "company_overview": "https://store.webkul.com/ WebKul Software Private limited is an ISO certified design and development company working on e-commerce and ERP from almost five years .We have served more than 1000+ e-commerce store world-wide and our modules are functioning more than 20000+ store world wide  .We have a very dedicated team on Opencart , Magento and Prestashop . So we understand e-commerce very deeply . We have developed some very complex solutions like from drop-shipping solutions to multi vendor marketplace  With their connection to our ERP programme .",
   "cover": {
      "cover_id": "10153641288963072",
      "offset_x": 0,
      "offset_y": 0,
      "source": "https://scontent.xx.fbcdn.net/hphotos-xpt1/v/t1.0-9/s720x720/15772_10153641288963072_644429492176708130_n.png?oh=df373f59bca9f7a26c353051d2940d18&oe=565685DB",
      "id": "10153641288963072"
   },
   "has_added_app": false,
   "is_community_page": false,
   "is_published": true,
   "likes": 7100,
   "link": "https://www.facebook.com/webkul",
   "location": {
      "city": "Noida",
      "country": "India",
      "street": "A-67 2nd Floor Sector 63 ",
      "zip": "201301"
   },
   "name": "WebKul",
   "parking": {
      "lot": 0,
      "street": 0,
      "valet": 0
   },
   "phone": "(+1)408-215-2638 ",
   "talking_about_count": 79,
   "username": "webkul",
   "website": "https://store.webkul.com/",
   "were_here_count": 0
}

 

Make sure you add ‘https://graph.facebook.com’ to Remote Site Settings to whitelist the URL.

Refer the below url for tips on parsing in Apex :- http://json2apex.herokuapp.com/

 

For Parsing in Apex Use the below class

public class JSON2Apex {

    public String id;
    public String about;
    public String awards;
    public Boolean can_post;
    public String category;
    public Integer checkins;
    public String company_overview;
    public Cover cover;
    public Boolean has_added_app;
    public Boolean is_community_page;
    public Boolean is_published;
    public Integer likes;
    public String link;
    public Location location;
    public String name;
    public Parking parking;
    public String phone;
    public Integer talking_about_count;
    public String username;
    public String website;
    public Integer were_here_count;

    public class Parking {
        public Integer lot;
        public Integer street;
        public Integer valet;
    }

    public class Location {
        public String city;
        public String country;
        public String street;
        public String zip;
    }

    public class Cover {
        public String cover_id;
        public Integer offset_x;
        public Integer offset_y;
        public String source;
        public String id;
    }

    
    public static JSON2Apex parse(String json) {
        return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
    }
    
    }

You can use this class, and deserialize the JSON by

JSON2Apex myClass = JSON2Apex.parse(str);

Now the Full code like this

Http http =new Http();
HttpRequest req =new HttpRequest();
req.setEndpoint('https://graph.facebook.com/webkul?access_token=CAAGIV8ndRDcBALpKhd5t5smOzFMv25HLpgqMqfZBhwZCJMbqPFW4aOZCXphud9tc7w9EEw0vBvRooZB7idLHLK3LkzS0pQP63pysUUZA7We5EV8IqZAsj7WwEhXf97C34aDKyaKW8lyvihyxl2duEpMjuHGs7LlsyPEZAwSJjykSpomgALOdvZBV');
req.setmethod('GET');
HttpResponse res = http.send(req);
String str = res.getbody();
System.debug(str);

JSON2Apex myClass = JSON2Apex.parse(str);

System.debug(myClass);

Now, the Output after deserialize the JSON given below

Best E-commerce OpenERP connector, can_post=true, category=Software, checkins=0, company_overview=https://store.webkul.com/ WebKul Software Private limited is an ISO certified design and development company working on e-commerce and ERP from almost five years .We have served more than 1000+ e-commerce store world-wide and our modules are functioning more than 20000+ store world wide  .We have a very dedicated team on Opencart , Magento and Prestashop . So we understand e-commerce very deeply . We have developed some very complex solutions like from drop-shipping solutions to multi vendor marketplace  With their connection to our ERP programme ., cover=Cover:[cover_id=10153641288963072, id=10153641288963072, offset_x=0, offset_y=0, source=https://scontent.xx.fbcdn.net/hphotos-xpt1/v/t1.0-9/s720x720/15772_10153641288963072_644429492176708130_n.png?oh=df373f59bca9f7a26c353051d2940d18&oe=565685DB], has_added_app=false, id=285021323071, is_community_page=false, is_published=true, likes=7100, link=https://www.facebook.com/webkul, location=Location:[city=Noida, country=India, street=A-67 2nd Floor Sector 63 , zip=201301], name=WebKul, parking=Parking:[lot=0, street=0, valet=0], phone=(+1)408-215-2638 , talking_about_count=73, username=webkul, website=https://store.webkul.com/, were_here_count=0]

Note** You can make Facebook Graph url like this :- https://graph.facebook.com/YOUR_PAGE_ID?access_token=YOUR_ACCESS_TOKEN

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home