I am using Facebook Auth on my local computer and it had been working fine until this morning. I can't figure out why I get an access denied when connecting.
http://facebook.com/en_US/all.js
Here is my script. I have checked it in several browsers.
window.fbAsyncInit = function() {
FB.init({
appId: '". $AppID."',
cookie: true,
xfbml: true,
oauth: true
});
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.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
Trying replacing window.loaction.reload() with window.location.reload(true).
If this doesn't fix the problem trying another way like:
window.location.replace(window.location.href)
Related
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
I'm having trouble with the javascript sdk for Facebook. I'm trying to do a simple api call which returns my name, according to the documentation. An alert should popup stating "Your name is ", but I'm getting "Your name is undefined" instead. Am I missing something?
The php SDK is working well, so I know it's not an error regarding app setup. I'm also currently logged into the app.
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId: '<app id>',
cookie: true,
xfbml: true,
oauth: true
});
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
});
FB.Event.subscribe('auth.logout', function(response) {
window.location.reload();
});
FB.api('/me', function(response) {
alert('Your name is ' + response.name);
});
};
(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>
Can any one tell me how exactly the FB API works.
It looks to be a basic question but I am really confused.
Question:
I have onlogin(). When I click the login button, I am expecting it to call this function.
But in the code I pasted: I see that alert-test is printed first and than the FB.api is called.
So, it looks like onlogin is called first, then the FB API ...is there a way I can call this function only once.
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({appId: 'XXX', status: true, cookie: true, xfbml: true});
};
(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);
}());
function checkFacebookLogin() {
FB.api('/me', function(response) {
alert("Name: "+ response.name + "\nFirst name: "+ response.first_name + "ID: "+response.id);
});
alert('test');
}
</script>
<p id="fb_login_button_1"><fb:login-button onlogin="checkFacebookLogin();" size="medium" scope="user_about_me">Sign in using Facebook</fb:login-button></p>
</body>v
My main issue is the function should be called only once....but it is getting called twice.
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({appId: 'XXX', status: true, cookie: true, xfbml: true});
};
(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);
}());
function fetchUserDetail()
{
FB.api('/me', function(response) {
alert("Name: "+ response.name + "\nFirst name: "+ response.first_name + "ID: "+response.id);
});
}
function checkFacebookLogin()
{
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
fetchUserDetail();
}
else
{
initiateFBLogin();
}
});
}
function initiateFBLogin()
{
FB.login(function(response) {
fetchUserDetail();
});
}
</script>
<input type="button" value="Sign in using Facebook" onclick="checkFacebookLogin();"/>
</body>
Request call to FB API is asynchronous. It means that once the request is sent, the code will not wait for the request to be completed. Because of this you get alert-test before the API Call is returned. All the FB API calls are asynchronous, if you try to make them synchronous then your browser will hang.
Thanks
Kaushik
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.
i have copied this code from the FB.init documentation:
<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/en_US" type="text/javascript"></script>
...
<!-- facebook JS SDK init -->
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init(
{
appId: '<?= FacebookHelper::FACEBOOK_APP_ID ?>',
status: true,
cookie: true,
xfbml: true
});
};
(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>
i placed it just before my </body> tag (as they recommend in the documentation), but i am getting this weird error (i can see via firebug):
b is undefined
[Break on this error] if(!window.FB)FB={};FB.forEach=functio...B.TypeLoader.subclass,FB.TypeLoader);
now, at first, i thought this must be a bug with my code, but the more i think about it, the more i remember hearing about some problems between firebug and facebook javascript. is that the problem, or am i forgetting something?
I think it is because you are including the older version of Facebook Connect. You don't need to include it with the newer JS SDK:
<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/en_US" type="text/javascript"></script>
Also, Facebook recommends placing the asynchronous loading code right after the opening body tag, such as (but it's your call):
<body>
<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 : true // parse XFBML
});
};
(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>
...
...
</body>
Firebug doesn't seem to be the issue here.
I had a similar problem with FB>UI calls for feed dialogues. My page was loaded via ajax and the UI does not poup. I just disabled the FireBug and it started to work amazingly!. Guess the FB is ambiguous between FireBug vs FaceBook for Mozilla.
Thanks all this issue is due to
<form id="fbform"></form>
<div id="fb-root"></div> is not included before script. you guys can try this code as i wrote.
div id="fb-root"></div>
<script>
//initializing API
window.fbAsyncInit = function() {
FB.init({appId: '298295206936650', status: true, cookie: true, xfbml: true});
};
(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>
<script>
//your fb login function
function fblogin(action) {alert('dd');
if(action == 'undefined'){
var action = 'fb_login';
}
FB.login(function(response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
var user = response;
//alert(user.birthday)
console.log(response)
//alert(user.id+'---'+user.email);
var picture_fb='https://graph.facebook.com/'+user.id+'/picture?width=100&height=100';
// alert(picture_fb);
if (user.current_location != null) {
var city = user.current_location['city'];
var country = user.current_location['country'];
}else{
var city = '';
var country = '';
}
if(action == 'update_fb_pic'){
var email_user= '<?=$this->session->userdata('user_id');?>';
}else{
if(user.email !='' && user.email !=''){
var email_user= user.email;
}else{
var email_user= '';
}
}
$.ajax({
type: 'POST',
url: "<?php echo site_url()?>front/ajax_fb_login",
data: "action="+action+"&fb_user_id="+user.id+"&username="+user.name+"&username="+user.name+"&first_name="+user.first_name+"&last_name="+user.last_name+"&gender="+user.gender+"&email="+email_user+"&avatar="+picture_fb+"&dob="+user.birthday,
success: function(msg){
//alert(msg);
//window.parent.document.location.reload();
window.location= "<?php echo site_url()?>profile"
//$("#fbform").submit();
}
});
});
} else {
console.log('User cancelled login or did not fully authorize.');
}
}, { scope: 'email,user_birthday,status_update,read_stream,publish_stream'});
}
</script>