Facebook Integration
Requirement: Post something on a facebook page using a custom button on the detail page of an sObject in Salesforce.
Please follow the below steps for Facebook Integration:
1. Login to Facebook.com and open developers.facebook.com. Click "Register".
2. Click on My Apps --> Add New App --> Choose Website --> Enter the required details.
3. Your app will be listed under the My Apps dropdown. Choose your app. You will be able to see the App ID and App Secret. Don't share these two with anyone.
4. Javascript code on the custom button:
window.open("https://www.facebook.com/dialog/oauth?client_id=[App ID]&display=popup&scope=manage_pages,publish_pages,publish_actions&response_type=token&redirect_uri={!URLENCODE([return URL])", target="_blank");
String pageId = '';
if(uarObj.data != null && uarObj.data.size() > 0)
{
for(pageTokenParser ptp: uarObj.data)
{
if(ptp.name == 'Test')//Test is the facebook page name on which you want to post
{
pageAccessToken = ptp.access_token;
pageId = ptp.id;
}
}
}
Please follow the below steps for Facebook Integration:
1. Login to Facebook.com and open developers.facebook.com. Click "Register".
2. Click on My Apps --> Add New App --> Choose Website --> Enter the required details.
3. Your app will be listed under the My Apps dropdown. Choose your app. You will be able to see the App ID and App Secret. Don't share these two with anyone.
4. Javascript code on the custom button:
window.open("https://www.facebook.com/dialog/oauth?client_id=[App ID]&display=popup&scope=manage_pages,publish_pages,publish_actions&response_type=token&redirect_uri={!URLENCODE([return URL])", target="_blank");
- App ID was created in Step-3. Return URL is the URL of any VF Page where we will do more callouts to post on facebook page.
- It will take the user to Facebook authentication page where the user will have to enter the facebook credentials. The application will ask for the permissions we have asked in the Scope parameter. Click okay to permit. The page will be redirected to the return URL.
- Access_Token will be received in the URL like this: https://cs18.salesforce.com/apex/facebookVfPage#access_token=12345678901234567890234567123
5. Retrieve User Access_Token from the URL, write this code on your VF page:
<apex:page standardController="Account" extensions="facebookVfPage">
<script type="text/javascript" >
window.onload = function()
{
console.log(window.location.hash);
facebookCallOut(window.location.hash);
};
</script>
<apex:form >
<apex:actionFunction name="facebookCallOut" action="{!facebookCallOut}" reRender="abc">
<apex:param name="param" value="" assignTo="{!fbHash}"/>
</apex:actionFunction>
<apex:outputPanel id="abc">
<apex:pagemessages ></apex:pagemessages><br/>
<div>
<apex:outputLink value="/{!Account.Id}" target="_self"
style="text-decoration:none;font-size:14px;color:blue;">
Go back to Account Record
</apex:outputLink>
</div>
</apex:outputPanel>
</apex:form>
</apex:page>
6. Retrieve Page Access_Token using the user Access Token, write this code in your apex class:
HttpRequest req = new HttpRequest();
req.setEndpoint('https://graph.facebook.com/me/accounts?access_token='+userAccessToken);
req.setMethod('GET');
Http http = new Http();
HTTPResponse res = http.send(req);
7. Parse the response using inner classes in your apex:req.setEndpoint('https://graph.facebook.com/me/accounts?access_token='+userAccessToken);
req.setMethod('GET');
Http http = new Http();
HTTPResponse res = http.send(req);
userAuthenticationResponseParser uarObj = (userAuthenticationResponseParser)JSON.deserialize(res.getBody(), userAuthenticationResponseParser.class);
String pageAccessToken = '';String pageId = '';
if(uarObj.data != null && uarObj.data.size() > 0)
{
for(pageTokenParser ptp: uarObj.data)
{
if(ptp.name == 'Test')//Test is the facebook page name on which you want to post
{
pageAccessToken = ptp.access_token;
pageId = ptp.id;
}
}
}
public class userAuthenticationResponseParser
{
public List<pageTokenParser> data;
}
public class pageTokenParser
{
public String access_token;
public String name;
public String id;
}
8. Final callout to post your message on facebook page:{
public List<pageTokenParser> data;
}
public class pageTokenParser
{
public String access_token;
public String name;
public String id;
}
HttpRequest req1 = new HttpRequest();
req1.setEndpoint('https://graph.facebook.com/v2.6/'+[pageId]+'/feed?access_token='+pageAccessToken+'&type=link');
req1.setMethod('POST');
req1.setBody('link=https://test.com');
Http http1 = new Http();
req1.setEndpoint('https://graph.facebook.com/v2.6/'+[pageId]+'/feed?access_token='+pageAccessToken+'&type=link');
req1.setMethod('POST');
req1.setBody('link=https://test.com');
Http http1 = new Http();
HTTPResponse res1 = http1.send(req1);
- pageId is the ID of your facebook page. It can be found on Facebook page --> Settings --> Page Info.
- For more API information, click "Docs" on the developer account of Facebook.
NOTE: Don't forget to add "graph.facebook.com" in Remote site settings.
First published on Blogger in 2016. Some screenshots and APIs have changed since; the code is kept as written.