Facebook registration issue - javascript

I am showing the facebook registration iframe in thick box (Iframe inside iframe).
(If user is not logged in, button will be shown to login or else registration form will be shown directly.)
Crom is caching data. Because of that after login to facebook it shows error like "invalid client_id" (It doesn't shows me registration form). If I reload the parent iframe ie. thickbox window registration form shows fine.
I want to reload the thickbox iframe after login to facebook. I can get this control some how?. Is there any JS code available for this ?
Here is my code
<div id="content">
<div class="reg-cont">
<div id="fb-root"></div>
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo $this->facebookAppId; ?>',
status : true,
cookie : true,
xfbml : true
});
FB.getLoginStatus(function(response) {
if (response.status == "connected") {
window.location = "/facebook-login?facebook_uid=" + response.session.uid;
}
});
};
</script>
<fb:registration
fields="[
{'name':'name'},
{'name':'email'},
{'name':'church', 'description':'Home Church', 'type':'select', 'options':<?php echo str_replace('"', "'", $this->church); ?>, 'default':'none'},
{'name':'host', 'description':'Check this box if you are hosting your small group', 'type':'checkbox'}
]"
redirect-uri="http://<?php echo $_SERVER['SERVER_NAME']; ?>/User/index/create-facebook-account"
fb_only="true"
width="530">
</fb:registration>
</div>
</div>

Reload a targeted iframe on login. You will need to set the link, and frame name below
FB.Event.subscribe('auth.login', function(response) {
top.frames['YourFrameName'].location.href = 'http://example.com/';
});
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo $this->facebookAppId; ?>',
status : true,
cookie : true,
xfbml : true
});
FB.getLoginStatus(function(response) {
if (response.status == "connected") {
window.location = "/facebook-login?facebook_uid=" + response.session.uid;
}
});
FB.Event.subscribe('auth.login', function(response) {
top.frames['YourFrameName'].location.href = 'http://example.com/';
});
};
</script>

Related

facebook register - how to make it work?

Although I am not new to programming or API's, I'm quite new to facebook API. This plugin uses some facebook code, and it's clearly not working.
I have following code on my registration page. However, the "register using facebook" is not clickable. This is what firebug shows: <fb:login-button scope="email,user_about_me,user_location"></fb:login-button>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo $ap_info['appId']; ?>',
status : true, // check login status
cookie : <?php echo $ap_info['cookie']; ?>, // enable cookies to allow the server to access the session
xfbml : true, // parse XFBML
oauth : true //enables OAuth 2.0
});
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
});
FB.Event.subscribe('auth.logout', function(response) {
window.location.reload();
});
};
(function() {
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
</script>
All the variables like appId, cookie etc have proper values. I debugged through them.
Have you tried the "official" sample code which is provided here?
https://developers.facebook.com/docs/facebook-login/login-flow-for-web/#quickstart

FB.Canvas.setSize(); don't work when changing page

I have a facebook tab with multiple page. Everything work fine on the first page, the iframe is resized perfectly, but on the second page where the content is less than on the first page the iframe won't move it stay the same height as the first page. At first it was alse staying at the bottom of the page but FB.Canvas.scrollTo(0,0); fixed that. I tryed calling setSize with a specific size and nothing work. Also tryed most of the solution on here but nothing work.
Here is the code that I'm calling on every page before I close the body
<script type="text/javascript">
window.fbAsyncInit = function() {
<cfif structKeyExists(cgi,'https') AND cgi.https IS 'ON'>
FB._https = true;
</cfif>
FB.init({
appId : '<cfoutput>#appId#</cfoutput>',
cookie : true, // enable cookies to allow the server to access the session
oauth : true, // OAuth 2.0
status : true, // check login status
xfbml : true // parse XFBML
});
FB.getLoginStatus(function(response) {
// user is connected but we don't have access to his province
if(typeof response.status == 'string' && response.status == 'connected') {
$('#erreurProvinceFB').css('display', 'block');
$('.fb_iframe_widget').css('display', 'none');
$('.sepOU').html(' ');
}
});
FB.Event.subscribe('auth.login', function(response) {
<!--- met en post le nouveau signedRequest car l'ancien qui est en session n'est plus valide et que
facbook ne renvoie pas un nouveau acces token si le tab est juste rafraichie et non reloader au complet. --->
$('#signed_request').val(response['authResponse']['signedRequest']);
$('#loginSignedRequest').submit();
});
FB.Canvas.setSize({ width: 520, height: 500 });
//FB.Canvas.setSize();
FB.Canvas.setAutoGrow();
FB.Canvas.scrollTo(0,0);
$("body").css("overflow", "hidden");
};
(function() {
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
This combination has worked well for me up to this point:
<body style="overflow: hidden;">
...
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '*ID*', // App ID
channelUrl : '/channel.php', // Channel File
status : false, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
if ( window.location.protocol == 'https:' ) {
FB._https = true;
}
FB.Canvas.setAutoGrow();
};
</script>
Ensure your FB App ID is correct.

Redirect URI using the Javascript SDK after authentication

So I've set up a basic login/authentication flow on the home page of my site. After the authentication dialog, I want to redirect them to another page on my site. I've tried several variations of the existing code, changing the site url under basic settings in the developer app, trying different oauth flows, and still, it simply redirects to the same homepage they logged in on. I know this sounds tremendously easy but there are so many different authentication flows and implementations that it's a bit confusing.
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'XXXXXXXXXX', // App ID
channelURL : '//localhost/dataweavr/channel.php', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
oauth : true, // enable OAuth 2.0
xfbml : true // parse XFBML
});
// Additional initialization code here
};
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
</script>
And then some more fun...
<div id="fb-root"></div>
<div id="user-info"></div>
<button id="fb-auth">Login</button>
<script>
window.fbAsyncInit = function() {
FB.init({ appId: 'XXXXXXXXXXXXXXXXXXXXXX',
status: true,
cookie: true,
xfbml: true,
oauth: true});
function updateButton(response) {
var button = document.getElementById('fb-auth');
if (response.authResponse) {
//user is already logged in and connected
var userInfo = document.getElementById('user-info');
FB.api('/me', function(response) {
userInfo.innerHTML = '<img src="https://graph.facebook.com/'
+ response.id + '/picture">' + response.name;
button.innerHTML = 'Logout';
});
button.onclick = function() {
FB.logout(function(response) {
var userInfo = document.getElementById('user-info');
userInfo.innerHTML="";
});
};
} else {
//user is not connected to your app or logged out
button.innerHTML = 'Login';
button.onclick = function() {
FB.login(function(response) {
if (response.authResponse) {
FB.api('/me', function(response) {
var userInfo = document.getElementById('user-info');
userInfo.innerHTML =
'<img src="https://graph.facebook.com/'
+ response.id + '/picture" style="margin-right:5px"/>'
+ response.name;
});
} else {
//user cancelled login or did not grant authorization
}
}, {scope:'email,user_birthday'});
}
}
}
// run once with current status and whenever the status changes
FB.getLoginStatus(updateButton);
FB.Event.subscribe('auth.statusChange', updateButton);
};
(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);
}());
</script>
And another unrelated issue. Why do I only have a normal button popup? How can I get the SDK to render a Facebook login button rather than just a simple login button?
You should be able to redirect the user after the login response. Under these lines:
FB.login(function(response) {
if (response.authResponse) {
Add something like this:
window.top.location = "http://page_to_redirect";
The button thing, hmm... well, you are writing a simple login button in the html, if you want a login button, change this:
Login
To this:
<fb:login-button></fb:login-button>
Hope it helps.

Facebook JavaScript SDK Login

Here's my code. It works perfectly on Facebook's JavaScript SDK test console, but not here : http://www.booktrolley.in/beta/fblogin.php
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head></head>
<body>
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({
appId : '270423476307006',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true, // parse XFBML
channelUrl : 'http://pedurology.org/booktrolley/beta/channel.html', // channel.html file
oauth : true // enable OAuth 2.0
});
</script>
<h1>Login, Logout, Name and Profile Picture</h1>
<div id="account-info"></div>
<script>
/**
* This assumes the user is logged in and renders their profile picture,
* name and a logout link.
*/
function showAccountInfo() {
FB.api(
{
method: 'fql.query',
query: 'SELECT name,uid,pic_square FROM user WHERE uid='+FB.getSession().uid
},
function(response) {
alert(FB.getSession().uid);
Log.info('API Callback', response);
document.getElementById('account-info').innerHTML = (
'<img src="' + response[0].pic_square + '"> ' +
response[0].name +
' <img onclick="FB.logout()" style="cursor: pointer;"' +
'src="https://s-static.ak.fbcdn.net/rsrc.php/z2Y31/hash/cxrz4k7j.gif">'
);
}
);
}
/**
* This assumes the user is logged out, and renders a login button.
*/
function showLoginButton() {
document.getElementById('account-info').innerHTML = (
'<img onclick="FB.login()" style="cursor: pointer;"' +
'src="https://s-static.ak.fbcdn.net/rsrc.php/zB6N8/hash/4li2k73z.gif">'
);
}
/**
* This will be called once on page load, and every time the status changes.
*/
function onStatus(response) {
Log.info('onStatus', response);
if (response.session) {
showAccountInfo();
} else {
showLoginButton();
}
}
FB.getLoginStatus(function(response) {
onStatus(response); // once on page load
FB.Event.subscribe('auth.statusChange', onStatus); // every status change
});
</script>
</body>
</html>
I'm getting a "Log is undefined" error in the following code:
function onStatus(response) {
Log.info('onStatus', response);
if (response.session) {
showAccountInfo();
} else {
showLoginButton();
}
}
FB.getLoginStatus(function(response) {
onStatus(response); // once on page load
FB.Event.subscribe('auth.statusChange', onStatus); // every status change
});
you trying to make use of a logger that exists on FB's page, but not on your own?
Try this code:
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head></head>
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId: '270423476307006',
status: true,
cookie: true,
xfbml: true,
oauth: true
});
FB.getLoginStatus(function(response) {
onStatus(response); // once on page load
FB.Event.subscribe('auth.statusChange', onStatus); // every status change
});
};
(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);
}());
</script>
<h1>Login, Logout, Name and Profile Picture</h1>
<div id="account-info"></div>
<script>
/**
* This assumes the user is logged in and renders their profile picture,
* name and a logout link.
*/
function showAccountInfo(resp) {
if( !resp.userID ) {
// we shouldn't be here
return;
}
FB.api(
{
method: 'fql.query',
query: 'SELECT name,uid,pic_square FROM user WHERE uid='+resp.userID
},
function(response) {
document.getElementById('account-info').innerHTML = (
'<img src="' + response[0].pic_square + '"> ' +
response[0].name +
' <img onclick="FB.logout()" style="cursor: pointer;"' +
'src="https://s-static.ak.fbcdn.net/rsrc.php/z2Y31/hash/cxrz4k7j.gif">'
);
}
);
}
/**
* This assumes the user is logged out, and renders a login button.
*/
function showLoginButton() {
document.getElementById('account-info').innerHTML = (
'<img onclick="FB.login()" style="cursor: pointer;"' +
'src="https://s-static.ak.fbcdn.net/rsrc.php/zB6N8/hash/4li2k73z.gif">'
);
}
/**
* This will be called once on page load, and every time the status changes.
*/
function onStatus(response) {
if (response.authResponse) {
showAccountInfo(response.authResponse);
} else {
showLoginButton();
}
}
</script>
</body>
Now let's explain what we've done:
We are using Asynchronous loading to insure that FB.getLoginStatus() will get fired ONLY when the library is successfully loaded
Since you are setting oauth to true then you need to change your code to work with the new responses (refer to this post) (you may need to change your app settings)
Based on #2, the current user ID to use in your showAccountInfo() will be accessible using response.authResponse.userID

Facebook JavaScript SDK unexpected behaviour

Here's m code :
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({
appId : '270423476xxxxx',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
</script>
<fb:registration
fields="name,birthday,gender,location,email"
redirect-uri="http://booktrolley.in/beta"
width="530">
Now if you go to my page on : http://www.booktrolley.in/beta/fbreg.php
There's this "Loading" animation that continues to be there, even after the page loads.Why is that so ?
Thanks
You are loading the plugin in a frame but your page's HTML structure is not correct, here's a better approach:
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head></head>
<body>
<div id="fb-root" > </div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script src="jquery.js"></script>
<script>
$(document).ready(function() {
var wid=$(document).width()-20;
$("#fb").attr('width',wid);
});
FB.init({
appId : 'XXXXXXXXXXX',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
</script>
<fb:registration redirect-uri="http://booktrolley.in/"
fields='[
{"name":"name"},
{"name":"foo","description":"Type foo","type":"text"},
{"name":"bar","description":"Type bar","type":"text"},
{"name":"facebooker","description":"Pick Paul","type":"select","options":
{"coder":"Paul","pm":"Austin","partners":"Cat"}},
{"name":"check","description":"Check this","type":"checkbox"},
{"name":"date","description":"Dec 16 2010","type":"date"},
{"name":"city","description":"Calgary","type":"typeahead","categories":
["city"]}]'
onvalidate="validate"></fb:registration>
<script>
function validate(form) {
errors = {};
if (form.foo !== "foo") {
errors.foo = "You didn't type foo";
}
if (form.bar !== "bar") {
errors.bar = "You didn't type bar";
}
if (form.facebooker !== "coder") {
errors.facebooker = "Pick the geeky one";
}
if (!form.check) {
errors.check = "Check the little box";
}
if (form.date !== '12/16/2010') {
errors.date = "That isn't the launch date";
}
if (form.city.id !== '111983945494775') {
errors.city = "That isn't Calgary, Alberta";
}
return errors;
}
</script>
</body>
</html>
This is what the ancient Egyptians called "a bug"

Categories

Resources