Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {scope: 'publish_stream'}
- // Click Handler for the login.
- $(document).delegate('#fb-connect', 'click', function(e){
- e.preventDefault();
- FB.login(function(response) {
- // Check the status
- // console.log(response.status);
- if (response.status === 'connected') {
- // Run the function we created below to publish to the users stream.
- publish('I just logged in via Facebook. '); // Your Message
- } else if (response.status === 'unknown' || response.status === 'not_authorized') {
- // Something else if they chose not to connect
- } else {
- }
- }, {scope: 'publish_stream'});
- });
- function publish(messagebody) {
- var body = messagebody; // This is the passed in string aka message you want to post.
- params = {
- message: body, // Your message body
- link: 'http://www.yoursite.com' // if you want to share a link.
- }
- FB.api('/me/feed', 'post', params, function(response) {
- if (!response || response.error) {
- //alert('Error occured');
- console.log(response.error);
- } else {
- // Successful post to facebook.
- // alert('Post ID: ' + response.id);
- }
- });
- }
- <html>
- <head>
- <title>Your Page</title>
- <!-- Load the Facebook SDK for your APP -->
- <script>
- window.fbAsyncInit = function() {
- // init the FB JS SDK
- FB.init({
- appId : 'YOURAPPID', // Test App ID for localhost
- channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel file for x-domain comms
- status : true, // Check Facebook Login status
- xfbml : true // Look for social plugins on the page
- });
- // Additional initialization code such as adding Event Listeners goes here
- };
- // Load the SDK asynchronously
- (function(d, s, id){
- var js, fjs = d.getElementsByTagName(s)[0];
- if (d.getElementById(id)) {return;}
- js = d.createElement(s); js.id = id;
- js.src = "//connect.facebook.net/en_US/all.js";
- fjs.parentNode.insertBefore(js, fjs);
- }(document, 'script', 'facebook-jssdk'));
- </script>
- </head>
- <body>
- <!-- Markup for Connect Button -->
- <a id="fb-connect">
- <img src="URLTOANIMAGEYOULIKEFORTHEBUTTON">
- </a>
- <!-- Reference to Jquery -->
- <script src="PATHTOJQUERY"></script>
- <!-- Some Additional Script to handle the functions / Could also be another JS file you include. -->
- <script>
- $(document).ready(function(){
- // Bind the Click of the Button fb-connect
- $(document).delegate('#fb-connect', 'click', function(e){
- e.preventDefault();
- FB.login(function(response) {
- // Check the status
- // console.log(response.status);
- if (response.status === 'connected') {
- // Run the function we created below to publish to the users stream.
- publish('I just logged in via Facebook. '); // Your Message
- } else if (response.status === 'unknown' || response.status === 'not_authorized') {
- // Something else if they chose not to connect
- } else {
- }
- }, {scope: 'publish_stream'});
- });
- });
- function publish(messagebody) {
- var body = messagebody; // This is the passed in string aka message you want to post.
- params = {
- message: body, // Your message body
- link: 'http://www.yoursite.com' // if you want to share a link.
- }
- FB.api('/me/feed', 'post', params, function(response) {
- if (!response || response.error) {
- //alert('Error occured');
- console.log(response.error);
- } else {
- // Successful post to facebook.
- // alert('Post ID: ' + response.id);
- }
- });
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement