Monday 20 August 2012

FACEBOOK INTEGRATION WITH SALESFORCE APEX LANGUAGE

FACE BOOK INTEGRATION WITH SALESFORCE USING APEX for websites



TO INTEGRATE WITH ANY  OUTHER PALTFORM FIRST WE HAVE TO REGISTER APPLICATION SO I AM  REGISTERIN G MY APPLICATION CHINNI123IN FACEBOOK 
NOTE
TO DO THIS AND IN YOUR SALESFORCE ACCOUNT GO TO ADMINSETUP- SECURITY CONTROLS -REMOTESITESETTINGS-GIVE THE  URLS HTTPS://WWW.FACEBOOK.COM
AND HTTPS://GRAPH.FACEBOOK.COM


CLICK  ON APPS AND 
THEN CLICK ON NEW APP THEN IT WILL ASK USERID PASSWARD GIVE
THEN
CLICK ON CREATE NEW APP 


 GIVE APP NAME 
AND APPNAMESPACE YOURWISE
THEN  CONTINUE


IN THAT GIVE DOMAIN NAME OF REDIRECT URL HERE MY DOMAIN IS
INTHE

Select how your app integrates with Facebook SECTION

CLICK
Website with Facebook Login GIVE THE SITE URL MEANS YOUR REDIRECT URL
MINE HERE IS


THEN YOU WILL GET APPID AND APP SECRET


USE THESE 2 IN YOUR PROGRAM



NOW THE CODE WILL START
FIRST WE HAVE TO AUTHENTICATE WITH FACEBOOK FOR THIS
USE THE URL
https://www.facebook.com/dialog/oauth
AND THE PARAMETERS

clint_id=application id or appid
redirect_uri=is  siteurl
state= any random string 
scope= optional but very important while working with full accessof facebook


copy the that url in browser address box

then the page will ask permistion CLICK ALLOW 

THEN THE PAGE REDIRECT TO YOUR SALESFORCE PAGE
AND CODE 

USE THE CODE
WITH

HttpRequest req=new HttpRequest();
    req.setMethod('GET');
//here client_id=appid and client_secrete=app secrete
//redirect_uri=site url
req.setEndpoint('https://graph.facebook.com/oauth/access_token?client_id=407057492687428&redirect_uri=https://chinnichinni-dev-ed.my.salesforce.com/apex/sts1&client_secret=0f1ccafdf93d8649567e529fe032451f&code=AQComasE6aDcb4j9_AiQghe-MgWNx7RWKxCPkB9kKmRn17Q_ac9jrU0WABl4zOQhQHnZx63c8J07jr44ic2DYn5tEmHaI_TEvcH5_qdaSGEGtGxIjtTNbCM4uy_9ub2FGB_Pr8x3T1T0WhG97i-OGdKX50PBc0usg38WQkCXp_QxaMHemotIINz6L_yUxWmcvck#_=_');

HttpResponse res = null;
      http h= new Http();
    res= h.send(req);
    
    system.debug( 'haiiiiiiiiiiiiiiiii'+res.getbody());



YOU WILL GET ACCESS TOKEN with expire time
o/p
access_token=AAAFyN3VBOkQBAOmwFSgd3yEptS4ZAq0JRT9ihjfcZCsgiPEwjyGxheaZAobVYsYDE7gWXLMJfBYWmlTmZAgqGFPONSEvnOvp0g6EosA84gZDZD&expires=5183856


use the access token to retrive data


HttpRequest req=new HttpRequest();
    req.setMethod('GET');

req.setEndpoint('https://graph.facebook.com/me?access_token=AAAFyN3VBOkQBAOmwFSgd3yEptS4ZAq0JRT9ihjfcZCsgiPEwjyGxheaZAobVYsYDE7gWXLMJfBYWmlTmZAgqGFPONSEvnOvp0g6EosA84gZDZD');

HttpResponse res = null;
      http h= new Http();
    res= h.send(req);
    
    system.debug( 'haiiiiiiiiiiiiiiiii'+res.getbody());

YOU WILL GET YOUR FACEBOOK DETAILS

to know your friends details

req.setEndpoint('https://graph.facebook.com/me/friends?access_token=AAAFyN3VBOkQBAOmwFSgd3yEptS4ZAq0JRT9ihjfcZCsgiPEwjyGxheaZAobVYsYDE7gWXLMJfBYWmlTmZAgqGFPONSEvnOvp0g6EosA84gZDZD');

HttpResponse res = null;
      http h= new Http();
    res= h.send(req);
    
    system.debug( 'haiiiiiiiiiiiiiiiii'+res.getbody());


to see albums use endpoint urls
https://graph.facebook.com/albums
to see specefied albums's photo
https://graph.facebook.com/albumid/photos
tosee posts
https://graph.facebook.com/me/feed

NEXT THE ABOVE ALL ARE A RETRIVE DATA FROM FACEBOOK 
TO
INSERT USE HTTP METHOD AS POST

HERE IT IS INSERTING PHOTO INTO MY FACEBOOK ALBUM USEING APEX CODE

HttpRequest req=new HttpRequest();
    req.setMethod('POST);
string sss='URL=http://www.vahrehvah.com/indianfood/wp-content/uploads/2010/11/JawaharlalNehru.jpg';
req.setbody(sss);
req.setheader('Content-type', 'application/x-www-form-urlencoded');
req.setHeader('Content-length',string.valueof(sss.length()) );
//albumid=281080795256517
    req.setEndpoint('https://graph.facebook.com/281080795256517/photos?access_token=AAAFyN3VBOkQBAOmwFSgd3yEptS4ZAq0JRT9ihjfcZCsgiPEwjyGxheaZAobVYsYDE7gWXLMJfBYWmlTmZAgqGFPONSEvnOvp0g6EosA84gZDZD');
HttpResponse res = null;
      http h= new Http();
    res= h.send(req);    
    system.debug( 'haiiiiiiiiiiiiiiiii'+res.getbody());



TO COMMeNT ON PHOTO
HttpRequest req=new HttpRequest();
    req.setMethod('POST);
string sss='message=hai ra mama';
req.setbody(sss);
req.setheader('Content-type', 'application/x-www-form-urlencoded');
req.setHeader('Content-length',string.valueof(sss.length()) );
    req.setEndpoint('https://graph.facebook.com/453946937969901/comments?access_token=AAAFyN3VBOkQBAOmwFSgd3yEptS4ZAq0JRT9ihjfcZCsgiPEwjyGxheaZAobVYsYDE7gWXLMJfBYWmlTmZAgqGFPONSEvnOvp0g6EosA84gZDZD');
HttpResponse res = null;
      http h= new Http();
    res= h.send(req);    
    system.debug( 'haiiiiiiiiiiiiiiiii'+res.getbody());


To DELETE A PHOTO
USE THE HTTPMETHOD DELETE


  HttpRequest req=new HttpRequest();
    req.setMethod('delete');
req.setEndpoint('https://graph.facebook.com/ALBUMID/PHOTOID?access_token=AAAFyN3VBOkQBAOmwFSgd3yEptS4ZAq0JRT9ihjfcZCsgiPEwjyGxheaZAobVYsYDE7gWXLMJfBYWmlTmZAgqGFPONSEvnOvp0g6EosA84gZDZD');
HttpResponse res = null;
      http h= new Http();
    res= h.send(req);    
    system.debug( 'haiiiiiiiiiiiiiiiii'+res.getbody());


WE CAN POST FROM APEX CODE
ENDPOINT URL IS
https://graph.facebook.com/me/feed