Created
July 23, 2012 16:50
-
-
Save metadaddy/3164674 to your computer and use it in GitHub Desktop.
Post to Facebook stream from Apex controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<apex:page controller="HelloController"> | |
<script src="https://connect.facebook.net/en_US/all.js"></script> | |
<div id="fb-root"></div> | |
<script> | |
// Get required permissions from the controller | |
var permissions = '{!permissions}'; | |
var permlist = permissions.split(','); | |
var reqperms = ''; | |
FB.init({appId: '{!appId}', xfbml: true, cookie: true}); | |
// We are logged in, but we need getLoginStatus() to get the FB JS | |
// lib to pick up the access token | |
FB.getLoginStatus(function(response) { | |
if (response.status === 'connected') { | |
// Check we have the permissions we want | |
FB.api('/me/permissions', function (response) { | |
for (var i = 0; i < permlist.length; i++) { | |
if (! response.data[0][permlist[i]]) { | |
if (reqperms !== '') { | |
reqperms += ','; | |
} | |
reqperms += permlist[i]; | |
} | |
} | |
if (reqperms.length > 0) { | |
// We need some permissions - ask for them | |
FB.login(function(response) { | |
if (!response) { | |
alert('Error getting permission!'); | |
} else { | |
location.reload(true); | |
} | |
}, {scope: reqperms}); | |
} | |
}); | |
} else if (response.status === 'not_authorized') { | |
// The user is logged in to Facebook, | |
// but has not authenticated the app | |
// We handle this in the controller | |
} else { | |
// The user isn't logged in to Facebook. | |
// We handle this in the controller | |
} | |
}); | |
</script> | |
<apex:pageMessages /> | |
<apex:form> | |
<apex:commandButton value="Hello" action="{!postHello}"/> | |
</apex:form> | |
</apex:page> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HelloController { | |
String access_token; | |
public PageReference postHello() { | |
Map<String,String> params = new Map<string,string>{'message' => 'Hello from Apex'}; | |
try { | |
FacebookPublish.postToWall(access_token, 'me', params); | |
} catch (FacebookException fbe) { | |
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, | |
fbe.getMessage()); | |
ApexPages.addMessage(myMsg); | |
} | |
return null; | |
} | |
public HelloController() { | |
// Replace the ID in this call with the ID of your auth provider | |
access_token = Auth.AuthToken.getAccessToken('AUTH_PROVIDER_ID', 'Facebook'); | |
} | |
public String getPermissions() { | |
return 'publish_stream'; | |
} | |
public String getAppId() { | |
// Replace this with your auth provider's consumer key | |
return 'FACEBOOK_APP_ID'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment