This bit of documentation has not been as helpful as you might think. I understand that I have to hang a display parameter on the end of the URL, but I'm not sure how to invoke a login window like that. Here's what I have now:
<script src="http://connect.facebook.net/en_US/all.js" type="text/javascript"></script>
<script type="text/javascript">
// initialize the library with the API key
FB.init({ apiKey: '{{ facebook_api_key }}', status: true, cookie: true, xfbml: true});
function facebookConnect(form){
function handleResponse(response){
form.submit();
}
FB.login(handleResponse,{perms:'publish_stream,user_about_me,status_update,email,offline_access'});
}
</script>
This works fine in a desktop browser, but I can't figure out how to get the 'touch' or 'wap' modes for dialogs.
I'm using django-socialregistration if that's of any relevance.
Logging into Facebook using a touch dialog used to work a while ago, but that same old code doesn't seem to work anymore.
However,
You can redirect a user to the login URL, and facebook will do the rest depending on the platform. The following snippet will login the user, and redirect back to your current page if set.
Update: You also need to edit your 'Basic App Settings' > 'Select how your app integrates with Facebook' and check 'Mobile Web' for this to work.
window.location = "http://www.facebook.com/dialog/oauth/"
+ "?" + "scope=" + "publish_stream,user_about_me,status_update,email,offline_access"
+ "&" + "client_id=" + YOUR_APP_ID_GOES_HERE
+ "&" + "redirect_uri=" + YOUR_CURRENT_PAGE_URL
+ "&" + "response_type=token";
The display:touch property can still be used in FB.ui() as follow (for instance, posting on a wall):
// Make sure you call FB.init() before FB.ui()
FB.ui({
app_id: YOUR_APP_ID_GOES_HERE,
method: 'feed',
name: "post title",
link: "http://link-to-what-you-are-sharing/",
picture: "your_image.jpg",
description: "post description",
display: 'touch'
},
function callback(r){
if ( (r && r.id) || (r && r.post_id) ){ alert('posted');}
else{ alert('failed'); }
});
Why are you wrapping the response handler inside a function?
Do something like :
function handleStatusChange(response) {
if (response.authResponse) {
console.log(response);
}
}
window.fbAsyncInit = function() {
FB.init({ appId: 'YOUR_APP_ID',
status: true,
cookie: true,
xfbml: true,
oauth: true
});
FB.Event.subscribe('auth.statusChange', handleStatusChange);
};
Taken from https://developers.facebook.com/docs/guides/mobile/web/
Related
I've been working on this for a few days and I can't find what's missing. Does anyone have any advice?
It seems like its missing something but I have no idea what it would be.
I'm not receiving any errors. The share button works (to share to your own wall) but the invites don't appear to do anything at all.
I've replaced my appid with X's.
<html>
<head>
<title>App Name</title>
</head>
<body>
<div id="fb-root"></div>
<script>
var publish = {
method: 'stream.publish',
display: 'popup', // force popup mode
attachment: {
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.'
),
href: 'http://app-address.com/'
}
};
function publish1() {
FB.ui(publish);//, Log.info.bind('stream.publish callback'));
}
</script>
<button class="btn" id="send-to-many">Send to Many</button>
<button onclick="publish1()">Click</button>
</body>
<script>
window.fbAsyncInit = function() {
console.log('Initing facebook...');
FB.init({
appId: 'xxxxxxxxxxxxxxx',
status: true,
frictionlessRequests: true,
cookie: true,
xfbml: true
});
console.log('... done');
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
document.getElementById('send-to-many').onclick = function() {
FB.ui({
method: 'apprequests',
message: 'This is a test of the App-Name invite.'
}, requestCallback);
function requestCallback(response)
{
if(response && response.request_ids) {
// Here, requests have been sent, facebook gives you the ids of all requests
//console.log(response);
location.href='home';
} else {
// No requests sent, you can do what you want (like...nothing, and stay on the page).
}
}
}
</script>
</html>
Your code is looking fine. There must be a error in your facebook app setting. For invite function you have to give 'Canvas URL' & 'Secure Canvas URL' in your facebook app setting.
You can also try with my codes below. This is a very simple code to implement.
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({
appId:'APP_ID',
cookie:true,
status:true,
xfbml:true
});
function FacebookInviteFriends()
{
FB.ui({
method: 'apprequests',
message: 'Your Message diaolog'
});
}
</script>
//HTML Code
<div id="fb-root"></div>
<a href='#' onclick="FacebookInviteFriends();">
Facebook Invite Friends Link
</a>
Here you have to write one more javascript code for breaking the iframe. So that the link 'll not open inside the facebook. You also have to write this same js code in your home page or any other page of your website, that you want your app to redirect.
<script type='text/javascript'>
if (top.location!= self.location)
{
top.location = self.location
}
</script>
For any further doubt feel free to write here.
I had a similar problem and found that you have to click the DONE button. This is a form and the DONE button submits the form.
I have a problem with my code that is meant to create the Facebook Connect login popup. In the code below, if I include the click event in my document ready then it triggers the creation of 2 popups, but if I leave it outside of the document ready then it fails to create any popup. Can anybody advise?
<script type="text/javascript">
$(document).ready(function () {
window.FB.init({ appId: 'xxx', status: true, cookie: true, xfbml: true });
});
$('#fbLogin').click(function () {
window.FB.login(function (response) {
if (response.status == 'connected') {
window.location.href = 'redirecturl';
}
}, { perms: 'email' });
});
</script>
<fb:login-button autologoutlink="false" perms="email" id="fbLogin">
</fb:login-button>
Clicking on fb:login-button will rise login dialog.
Your binding on #fblogin will open second dialog
You need to choose one of fb:login-button or FB.login not both.
If you need page redirected once user is logged in just remove the FB.login call and subscribe (FB.Event.subscribe) to auth.statusChange event to check login status.
FB.Event.subscribe('auth.statusChange', function(response){
if (response.status == 'connected') {
window.location.href = 'redirecturl';
}
});
I have tried putting together some code based on some other SO questions and various web tutorials.
The error I am getting is right at the end when I try and post to my friend's walls.
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<p><input type="button" onclick="sendRequestViaMultiFriendSelector(); return false;" value="Recommend friends for another chance to win!" /></p>
<div id="response"></div>
<script>
FB.init({
appId : 'xxxxxxxxxx',
status : true,
cookie : true,
oauth: true
});
function sendRequestViaMultiFriendSelector() {
FB.ui({method: 'apprequests',
message: 'My Great Request'
}, requestCallback);
}
function requestCallback(response) {
// Handle callback here
if (response.request && response.to) {
var request_ids = [];
for(i=0; i<response.to.length; i++) {
var temp = response.request + '_' + response.to[i];
var friend_id = response.to[i];
request_ids.push(temp);
//send message in here
var opts = {
message : 'Message',
name : 'Name',
link : 'http://www.facebook.com/pages/xxxxx/xxxxxxxxxxxxxxx',
description : 'Description here',
picture : 'http://www.example.com/img/some.jpg'
};
FB.api('/'+friend_id+'/feed', 'post', opts, function(response) {
if (!response || response.error) {
// This is the error I am getting:
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});
// end send message
}
var requests = request_ids.join(',');
} else {
alert('Didn't send recommendation to friends');
}
}
</script>
The code is actually working great, it worked as soon as I granted myself the access token in graph api explorer.
Just need to set up my auth dialogue now, lots of fun!
Just thought I'd better answer this incase anyone else runs into the same situation.
I want my users to be able to share dynamic content on their Facebook news feed. There's no other Facebook integration (e.g. Facebook login or other server-side integration) so I want this to be as light as possible.
This is where I got, but how does it looks like? It seems to work, but I'm not sure if I've thought of everything.
<button id="fb-publish">Share to Facebook</button>
<script type="text/javascript">
(function() {
FB.init({
appId: MY_FACEBOOK_APP_ID, cookie: true, status: true, xfbml: true, oauth: true
});
var fbAuth = null;
var fbShare = function() {
FB.ui({
method: "feed",
display: "iframe",
link: "http://example.com/",
caption: "Example.com",
description: "Here is the text I want to share.",
picture: "http://example.com/image.png"
});
};
$("#fb-publish").click(function() {
if (!fbAuth) {
FB.login(function(response) {
if (response.authResponse) {
fbAuth = response.authResponse;
fbShare();
}
}, {scope: 'publish_stream'});
} else {
fbShare();
}
});
})();
</script>
Also, if I want to attach an image from a URL (bigger than just the 50x50 pixel image defined in the picture field), how can I do that with just JavaScript? Or can I?
I decided to leave out authResponse caching and call FB.login() every time. It briefly opens a popup window, but at least there's no case where user has logged out in another tab or window and the cached authResponse has gone stale. The briefly flashing popup window is not that big of an issue, since I don't think users will share the same page multiple times, anyway.
Here's my final code which is even simpler than the original one:
<button id="fb-publish">Share to Facebook</button>
<script type="text/javascript">
(function() {
FB.init({
appId: MY_FACEBOOK_APP_ID, cookie: true, status: true, xfbml: true, oauth: true
});
var fbShare = function() {
FB.ui({
method: "feed",
display: "iframe",
link: "http://example.com/",
caption: "Example.com",
description: "Here is the text I want to share.",
picture: "http://example.com/image.png"
});
};
$("#fb-publish").click(function() {
FB.login(function(response) {
if (response.authResponse) {
fbShare();
}
}, {scope: 'publish_stream'});
});
})();
</script>
And as mentioned by Tolga Arican, if it's ok to you that the sharing dialog opens in a popup window, you don't even need to call FB.login() and ask for the publish_stream permission; just call FB.ui({ method: "feed" }) directly and you're good to go:
<button id="fb-publish">Share to Facebook</button>
<script type="text/javascript">
(function() {
FB.init({
appId: MY_FACEBOOK_APP_ID, cookie: true, status: true, xfbml: true, oauth: true
});
$("#fb-publish").click(function() {
FB.ui({
method: "feed",
link: "http://example.com/",
caption: "Example.com",
description: "Here is the text I want to share.",
picture: "http://example.com/image.png"
});
});
})();
</script>
1) Make sure appId defined.
2) In FB.login , response.authResponse may not work. I'm not pretty sure but just throw console.log, it may response.session
3) in method:feed , you can put larger images for picture, but it wil downsize. There is no way to put big images as a feed thumbnail. It is just a thumb of a feed..
For the lightest method:feed js, you don't need display:iframe i guess.
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.' }, function(response) {} });
I have a bug while using FB.ui, although I'm doing everything by the book.
I want to a button so users can share the page on their wall.
When I click the button, I get a facebook popup window that says:
An error occurred, Please try again later.
Can you see what's wrong?
Thanks!
Here's my code:
<script type="text/javascript" src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init
({
appId:'MYAPPID',
status:true,
cookie: true,
xfbml:true
});
function shareProject2()
{
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) {
getPermission();
} else {
alert('Post was not published.');
}
});
return false;
}
</script>
<a href="#" onclick="shareProject2();" >Share</a>
Have you tried passing the session from the server to the client JavaScript
This would be done in php as:
<?php
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => $appId, // Your App Id
'secret' => $secret, // Your App Secret
'cookie' => true,
));
$session = $facebook->getSession();
?>
<script>
FB.init
({
appId:'MYAPPID',<?php
echo (!empty($session)) ? 'session: ' . json_encode($session) . ',' : '' ?>
status:true,
cookie: true,
xfbml:true
});
...
</script>