Twitter and facebook share with callbacks? - facebook

I have implemented facebook and twitter share to my website. The users are redirected to the following URLs when they click on the twitter or facebook share button.
http://www.facebook.com/sharer.php?u='+encodeURIComponent(location)+'&t='+encodeURIComponent(text)
http://twitter.com/share?url='+location+'&text='+text
I want to store the username and tweet link if possible in my database when someone the content through twitter and I want to store facebook profile url and name when someone shares it through facebook. Is it possible to retrive that data when someone has successfully completed the share process?

The facebook javascript API allows you to share and gives you a callback
http://developers.facebook.com/docs/reference/javascript/FB.ui/
FB.ui(
{
method: 'feed',
name: 'Facebook Dialogs',
link: 'http://developers.facebook.com/docs/reference/dialogs/',
picture: 'http://fbrell.com/f8.jpg',
caption: 'Reference Documentation',
description: 'Dialogs provide a simple, consistent interface for applications to interface with users.',
message: 'Facebook Dialogs are easy!'
},
function(response) {
if (response && response.post_id) {
alert('Post was published.');
} else {
alert('Post was not published.');
}
}
);
To use this you need to have set up a facebook application and put the following code directly after the opening body tag
<div id="fb-root">
</div>
<script src="http://connect.facebook.net/en_US/all.js" type="text/javascript"></script>
<script type="text/javascript">
FB.init({
appId: YOUR_API_KEY,
status: true,
cookie: true,
xfbml: true
});
FB.Canvas.setAutoResize();
</script>
If you ask for basic permissions, you could then get the users facebook id and use this to store which users had shared.

For Twitter call with callback, that is how I did it in Javascript:
// Include the Twitter Library
window.twttr = (function (d,s,id) {
var t, js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return; js=d.createElement(s); js.id=id;
js.src="https://platform.twitter.com/widgets.js"; fjs.parentNode.insertBefore(js, fjs);
return window.twttr || (t = { _e: [], ready: function(f){ t._e.push(f) } });
}(document, "script", "twitter-wjs"));
// On ready, register the callback...
twttr.ready(function (twttr) {
twttr.events.bind('tweet', function (event) {
// your callback action here...
});
});
The twitter link is like this:
Share on Twitter
Reference: https://dev.twitter.com/docs/intents/events

Once you open the FB share window, is up to the user to publish the content or not. There is no callback function and it's not possible to know if he has completed the proccess.
You may want to implement a FB connect and create your own form to publish into the user's wall once he has filled the fields (and also you have the option to save them in your DB).
I don't know if Twitter offers the feature that you are requesting, but I would go for the same solution as above.

For Facebook, there is an updated Share Dialog with callback (Source: https://developers.facebook.com/docs/sharing/reference/share-dialog)
https://www.facebook.com/dialog/share?
app_id=145634995501895
&display=popup
&href=https%3A%2F%2Fdevelopers.facebook.com%2Fdocs%2F
&redirect_uri=https%3A%2F%2Fdevelopers.facebook.com%2Ftools%2Fexplorer
You must provide an app_id, and the redirect_uri must be whitelisted in your App domains.

Related

how do i log in to my website using facebook social login

I am stuck with loggin into my website using facebook but i keep getting this this problem that when ever i click on the facebook log in button the pop up shows and hides immediately
i already tried using the facebook document code for web login
<!DOCTYPE html>
<html>
<head>
<title>Facebook Login JavaScript Example</title>
<meta charset="UTF-8">
</head>
<body>
<script>
// This is called with the results from from FB.getLoginStatus().
function statusChangeCallback(response) {
console.log('statusChangeCallback');
console.log(response);
// The response object is returned with a status field that lets the
// app know the current login status of the person.
// Full docs on the response object can be found in the documentation
// for FB.getLoginStatus().
if (response.status === 'connected') {
// Logged into your app and Facebook.
testAPI();
} else {
// The person is not logged into your app or we are unable to tell.
document.getElementById('status').innerHTML = 'Please log ' +
'into this app.';
}
}
// This function is called when someone finishes with the Login
// Button. See the onlogin handler attached to it in the sample
// code below.
function checkLoginState() {
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
}
window.fbAsyncInit = function() {
FB.init({
appId : '{your-app-id}',
cookie : true, // enable cookies to allow the server to access
// the session
xfbml : true, // parse social plugins on this page
version : 'v2.8' // use graph api version 2.8
});
// Now that we've initialized the JavaScript SDK, we call
// FB.getLoginStatus(). This function gets the state of the
// person visiting this page and can return one of three states to
// the callback you provide. They can be:
//
// 1. Logged into your app ('connected')
// 2. Logged into Facebook, but not your app ('not_authorized')
// 3. Not logged into Facebook and can't tell if they are logged into
// your app or not.
//
// These three cases are handled in the callback function.
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
};
// 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/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
// Here we run a very simple test of the Graph API after login is
// successful. See statusChangeCallback() for when this call is made.
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Successful login for: ' + response.name);
document.getElementById('status').innerHTML =
'Thanks for logging in, ' + response.name + '!';
});
}
</script>
<!--
Below we include the Login Button social plugin. This button uses
the JavaScript SDK to present a graphical Login button that triggers
the FB.login() function when clicked.
-->
<fb:login-button scope="public_profile,email" onlogin="checkLoginState();">
</fb:login-button>
<div id="status">
</div>
</body>
</html>
i used this link below
https://developers.facebook.com/docs/facebook-login/web
thanks for your help
Facebook Social Plugin: The Login Button Facebook offers a host of what they call social plugins. The one we are interested in is the Login Button. Here are the basic steps involved in getting the button onto your webpage:
You need a Facebook app for your site. Go to https://developers.facebook.com/apps/ and click on the "New App" button.
Your "App Display Name" can be something meaningful. I usually go with the domain name for the site. You can use this same app for other purposes in the future as well. The "App Namespace" needs to be something that does not contain any special characters. I usually use my app display name without spaces, periods, etc. Now you need to enter your "App Domain" and Website "Site URL." For the app domain enter the domain name on which your site is hosted. For the site URL enter the full URL of your website. For example, if your domain is name.com then your site URL should be something like http://www.your_domain.com. Save your changes once you have the proper information entered. Now you will have a page that shows yours "App ID" and "App Secret". Just leave that page open because you will need your app ID when you add the login button code to your page. Now we need to go get the login button code from Facebook. Go to http://developers.facebook.com/docs/reference/plugins/login/ and click the "Get Code" button. Copy the HTML5 code from the first box and for now put it just below the body tab of your webpage. The code should look something like this:
<div id="fb-root"></div>
<script>
(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#xfbml=1&appId=APP_ID";
fjs.parentNode.insertBefore(js, fjs);
} (document, 'script', 'facebook-jssdk'));
</script>
In the code be sure to replace APP_ID with the app id you created for your Facebook app. Now paste the code from the second box in whatever place you want the login button to reside. The code should look something like:
<div class="fb-login-button" data-show-faces="false" data-width="200" data-max-rows="1"></div>
data-show-faces equals false because with it set to true the logout button will show when the user is logged in.

Facebook share with custom parameters with API version 2.9

I need to share a result of a quiz on facebook containing a custom title, picture and description. Wich worked perfect before update to version 2.9 on April 18.
But it isn't working with Version 2.9. So am I missing out something there? Or does Facebook don't want us to share custom Facebook Feeds of our websites in 2017?
For my Setup for a test facebook feed I coded strictly with Facebook Developers Documentation.
Facebook Changelog v2.9 says parameter picture, name, description and caption are not supported anymore.
My fb api init:
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '*APP-ID*',
xfbml : true,
version : 'v2.9'
});
FB.AppEvents.logPageView();
};
(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/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
My fb-share call:
<script>
$(document).ready(function() {
$('#fbButton').click(function() {
FB.ui({
method: 'feed',
link: '*URL*',
caption: 'An example caption',
picture: '*URL*/img/content4.jpg',
name: 'An example name',
description: 'An example description'
}, function(response){});
});
});
</script>
As a result I get a facebook feed without any picture or description. The only thing is a title (the title of page defined in the <head> with <title>).
Obviously, the usual procedure isn't working anymore.
So is there any way to share custom texts with custom pictures with the new facebook API version 2.9?
Is there any workaround? Or is it just impossible with v2.9, because facebook doesn't want us to share custom feeds like this? (for whatever reason..)
(and no, I cannot use og:tags)
I just tried to use open graph method instead of feed and override og properties, see below:
FB.ui({
method: 'share_open_graph',
action_type: 'og.shares',
action_properties: JSON.stringify({
object : {
'og:url': 'http://astahdziq.in/', // your url to share
'og:title': 'Here my custom title',
'og:description': 'here custom description',
'og:image': 'http://apps.investis.com/Dharmendra/fbPOC/south.jpg'
}
})
},
// callback
function(response) {
if (response && !response.error_message) {
// then get post content
alert('successfully posted. Status id : '+response.post_id);
} else {
alert('Something went error.');
}
});
this worked for me as I am able to post custom image with title and description.
Just encounter with this problem myself.
After some research I found this solution:
www.facebook.com/sharer.php?caption=[caption]&description=[description]&u=[website]&picture=[image-url]
you can use JavaScript window.open to simulate the behavior like this:
Here is a working example:
https://jsfiddle.net/ovidiu_turean/mhuexvjL/
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
window.open('https://www.facebook.com/sharer.php?title=ThisIsAtitle&description=ThisIsAdesc&u=https://www.google.ro&picture=https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-526588.jpg', 'Share', 'scrollbars=yes,resizable=yes,toolbar=no,menubar=no,scrollbars=no,location=no,directories=no,width=300, height=300, top=300, left=300' );
}
</script>
https://developers.facebook.com/docs/apps/changelog#v2_9
Custom parameters are not possible anymore, shared URLs only take their data from the Open Graph Tags on the website, Open Graph Tags need to be static for each URL. Else, you could share ANY URL with ANY title/description, which could be misleading.

How to check if user shared website (Facebook)? Is it possible?

I am currently writing a website and I need some help about facebook integration.
I need a function (PHP or JS, both will help) that can check if a given user shared my website,
and I couldn’t find out how to write one.
Could you please point me at the right direction?
First you have to load the Facebook SDK right after your tag:
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : "YOUR APP ID",
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : false, // parse XFBML
perms : 'read_stream',
access_token : "USER ACCESS TOkEN",
frictionlessRequests : true
});
};
// 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/pt_BR/all.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
Then you can use the callback function to do what you want:
<script type="text/javascript">
function shareOnFacebook() {
FB.ui(
{
method : 'feed',
display : 'iframe',
name : 'name',
link : 'http://www.linktoshare.com',
picture : 'http://www.linktoshare.com/images/imagethumbnail.png',
caption : 'txt caption',
description : 'txt description',
access_token : 'user access token'
},
function(response) {
if (response && response.post_id) {
// HERE YOU CAN DO WHAT YOU NEED
alert('OK! User has published on Facebook.');
} else {
//alert('Post was not published.');
}
}
);
}
</script>
Then you should use it like this:
Share on facebook
Using the JavaScript SDK, FB.ui() and the feed dialog, you can prompt your users to share a URL on Facebook. This dialog provides a callback function so that you can tell if the post was made successfully.
Code sample lifted from the above link...
var obj = {
method: 'feed',
link: 'https://developers.facebook.com/docs/reference/dialogs/',
picture: 'http://fbrell.com/f8.jpg',
name: 'Facebook Dialogs',
caption: 'Reference Documentation',
description: 'Using Dialogs to interact with users.'
};
function callback(response) {
document.getElementById('msg').innerHTML = "Post ID: " + response['post_id'];
}
FB.ui(obj, callback);
This is the easiest way to accomplish what you need.
It will not, however, allow you to test if someone shared your website's URL outside of your feed dialog.
A more complex alternative not requiring the dialog callback could be accomplished by utilizing the read_stream permission. Once you have that permission, you can scan the users past posts to see if he has ever shared your website on his wall...
Keep in mind that this will not work if the user shared your website on some one else's wall or on a page...

FB.ui is giving me Error Message: redirect_uri is not owned by the application

Help please! I'm creating a Facebook tab/app on facebook for a contest, part of the contest is to have a user have the option to share the contest on their wall. This is the error I'm receiving -
API Error Code: 191
API Error Description: The specified URL is not owned by the application
Error Message: redirect_uri is not owned by the application.
This is my facebook auth code:
<div id='fb-root'></div>
<script>
FB.init( {
appId : 219691398094935,
status : true,
cookie : true,
xfbml : true
}
);
window.fbAsyncInit = function() {
FB.Canvas.setAutoResize();
}
</script>
<script src='http://connect.facebook.net/en_US/all.js'></script>
This is my FB.ui Code:
<script>
function showStreamPublish() {
FB.ui(
{
method: 'feed',
name: 'I signed up to win an iPad 2 from Upillar.com',
link: 'http://www.facebook.com/pages/udev/121216321302968?sk=app_219691398094935',
picture: 'http://static.upillar.com/fb/images/logo.png',
description: 'I entered the iPad 2 Sweepstakes from Upillar.com which only requires you to create a listing to enter! Enter the contest now by by clicking this link.',
app_id: '219691398094935'
},
function(response) {
if (response && response.post_id) {
$('#main-screen').css('display','none');
$('#step2').css('display','block');
}else{
$('#main-screen').css('display','none');
$('#step2').css('display','block');
}
});}
</script>
This is the information in my app:
Site/App Domain: upillar.com
Site URL: http://fb.upillar.com/contest.php
Page Tab URL: http://fb.upillar.com/contest.php
Secure Tab URL: https://fb.upillar.com/contest.php
Please tell me what am I missing! Thank you so much.
Got same problem - solution is simple, but not even mentioned by Facebook Dev DOCS. Take a look at this:
FB.ui(
{
method: 'feed',
redirect_uri: 'http://www.yourdomain.com', // that caused the problem
message: 'getting educated about Facebook Connect',
name: 'Connect',
caption: 'The Facebook Connect JavaScript SDK',
description: (
'A small JavaScript library that allows you to harness ' +
'the power of Facebook, bringing the user\'s identity, ' +
'social graph and distribution power to your site.'
),
link: 'http://www.yourdomain.com',
picture: 'http://www.yourdomain.com/f8.jpg',
actions: [{
name: 'yourdomain',
link: 'http://www.yourdomain.com'
}],
user_message_prompt: 'Share your thoughts about RELL'
},
function (response) {
if (response && response.post_id) {
alert('Post was published.');
} else {
alert('Post was not published.');
}
}
);
site URL should not be a file, try to set this as your domain only, without contest.php
if that does not work, set the canvas URL as your domain, with "/" at the end too.
Sorry to bump an old post, first I should say i'm relatively new to facebook development but i've been getting the same problem, basically it seems like facebook does some fairly nifty detection in terms of what qualifies as a redirect when you land on your page.
For example I was detecting if it was a new user and then displaying a colorbox, whose onclick event redirected to a newuser creation form. That gave me error 191 redirect_uri not owned. It seems because the javascript was in the <HEAD> element it qualified as a redirect.
I simply changed the login process to fully render the main page and display a normal div telling them to click here to login.
Sorry for grave-digging!
I just had the same problem and found the solution. You do not have to change anything on your sources.
Just head over to http://developers.facebook.com open your app and navigate to "Settings" and "Basic".
There is a textfield that says "App Domains" - enter the domain name where the app is currently hosted at there, save the settings and you are done.

Facebook iFrame app - displaying FB:ui depending on whether a user is logged in or not?

I'm creating an iFrame app on Facebook which is basically just a Mailing List sign up page. However, when people click Sign Up, the FB:ui "Post To Your Wall" pops up and gives the user the option to share what they have just signed up for if they want to. It works fine.
However, I want to find a way to display the FB:ui ONLY to users who are logged into Facebook. My app shouldn't require any permission from users as it is simply a landing page that gives people an option to share if they want to. There's never any automatic sharing done.
I'm using the below code currently:
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js">
</script>
<script>
FB.init({
appId:'my app id', cookie:true,
status:true, xfbml:true
});
FB.getLoginStatus(function(response) {
if(response.session) {
sharethisnow();
} else {
// no user session available, someone you dont know
if(response.status == "notConnected") {
// But user is logged into facebook
sharethisnow();
} else {
// User is not logged into facebook
}
}
});
function sharethisnow() {
FB.ui({ method: 'feed',
link: 'my link',
name: 'my page name',
caption: 'my caption',
description: 'my decription',
picture: 'my picture',
message: 'my message'});
}
</script>
As you can see, I'm attempting to run the FB:ui (sharethisnow function) only to users who are either logged into Facebook AND my app, or logged into Facebook only. But for some reason the code above is only working for page-admins. When I log in as an admin, I sign up and the FB:ui pops up as it should do. However, when I view the page as a non-admin (just a general logged in Facebook user), the FB:ui pops up but says that there is an error and to "please try again later".
It's working fine for me, maybe check the Page settings (private/public). Also try this simpler code first:
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({
appId:'APP_ID', cookie:true,
status:true, xfbml:true
});
FB.getLoginStatus(function(response) {
if(response.status == "unknown") {
return;
}
sharethisnow();
});
function sharethisnow() {
FB.ui({
method: 'feed',
message: 'my message'
});
}
</script>