I have implemented a simple facebook login script for my facebook page tab app. It supposes to authenticate the app to the user. It works fine in all browsers, but IE (I have IE10 for testing the compatibility). In IE it brings the authentication pop-up as it intended and user can authenticate it just fine, but then instead of opening the main app page (liked.php) as it does in other browsers it keeps reloading itself (login.php). I know no one should be using IE in our days, but some people does and I need it to work even for them:)
Thank you! The code is here:
<body>
<div id="fb-root"></div>
<fb:login-button size="medium" onlogin="login()" scope="email, user_birthday">Logga in med Facebook</fb:login-button>
<script>
// Additional JS functions here
window.fbAsyncInit = function() {
FB.init({
appId : 'myid', // App ID
channelUrl : '//www.mydomain.com/channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
// Additional init code here
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// connected
} else if (response.status === 'not_authorized') {
// not_authorized
//login();
} else {
// not_logged_in
//login();
}
});
};
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
function login() {
FB.login(function(response) {
if (response.authResponse) {
// connected
window.location = 'liked.php';
} else {
// cancelled
}
});
}
</script>
</body>
And this is the answer to my problem which as I noticed most Facebook connect solutions for tab apps (iframe in FB) have in IE10:
https://stackoverflow.com/a/7083831/2169059
Related
I have the following code for my Facebook Application:
window.fbAsyncInit = function() {
FB.init({
appId : 'XXXXXX', // App ID
channelUrl : '/channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
// Additional initialization code here
//***************************************************
//* FanGate for Facebook
//***************************************************
var hideLogin = function(){
$("#login-fb").hide();
}
var showLogin = function(){
$("#login-fb").show();
}
var doLogin = function(){
FB.login(function(response) {
if (response.session) {
hideLogin();
checkLike(response.session.uid)
} else {
// user is not logged in
}
});
}
var checkLike = function(user_id){
var page_id = "XXXXXXXXX"; //coca cola
var fql_query = "SELECT uid FROM page_fan WHERE page_id = "+page_id+"and uid="+user_id;
var the_query = FB.Data.query(fql_query);
the_query.wait(function(rows) {
if (rows.length == 1 && rows[0].uid == user_id) {
$("#thirsty_thursdays").show();
} else {
$("#fan_gate").show();
}
});
}
FB.getLoginStatus(function(response) {
console.log(response.status);
if (response.status == 'connected') {
hideLogin();
checkLike(response.authResponse.userID)
} else {
showLogin();
}
}, true);
$("#login-fb a").click(doLogin);
};
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
response.status always returns not_authorized and I am really unclear why. I am logged into Facebook and the application is a simple Page Tab. I have recently added App on Facebook to the setting to see if that helps but no luck.
I have not seen any additional info on the Facebook JS SDK docs that give any info.
I am basically looking to show content if the user is logged in and is a fan of a certain page and hide the content if not. (commonly called fan gate)
Would have posted this as a comment but I cant yet...
After your tab loads and the FB.getLoginStatus() response indicates "not_authorized" (and therefore, I assume, shows your login button) -- what happens if you then click your login button? Is it giving the facebook pop-up prompt to authorize the app? (I'm hesitant to ask this but are you in fact sure that you've authorized the app?) Or does it just do nothing?
-In the callback for FB.login.. what does it output if you add a console.log(response.authResponse) ?
-I believe the channelUrl is supposed to be fully qualified whereas you have it root relative.. Not sure that would have any impact on your issue but might want to try it.. see https://developers.facebook.com/docs/javascript/gettingstarted/#channel
I can't seem to get the permissions for my facebook connection. Here's what I have:
window.fbAsyncInit = function() {
FB.init({
appId : 'myID', // App ID
channelUrl : 'myChannel', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true, // parse XFBML
oauth : true
});
FB.Event.subscribe('auth.authResponseChange', function(response) {
if (response.status === 'connected') {testAPI();}
if (response.status === 'not_authorized') { FB.login(function(response) {}, {scope: 'email,user_likes'});}
else { FB.login(function(response) {}, {scope: 'email,user_likes'});}
});
};
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
When I hit my login button it asks:
MyApp would like to access your public profile and friend list.
What have I done wrong?
When you set the scope you are not giving permissions to the app. You are only telling that permissions that your app will need.
When you click in the login button for the first time, that dialog saying that "MyApp would like to access your public profile and friend list." appear. You need to confirm that you grant that permissions to your app by clicking in the ok button or similar in that dialog.
You should not need to to this again unless you change your application id or key or you manually revoke the authorization for that app in the facebook settings.
I have the following simple code for facebook website login below. I tested the site in a mozilla browser and everything worked fine, opening up a prompt for me to login to facebook, but when I tried again on a windows OS with chrome, my button does not do anything. This is especially frustrating because it looks like the button works - but only sometimes, and I have no idea why. I am hosting my site through google app engine.
I've been trying to figure this out for ages, any help would be greatly appreciated!!
// Additional JS functions here
window.fbAsyncInit = function() {
FB.init({
appId : 'xxx', // App ID
channelUrl : '//http://www.XXX.com/channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
};
function doLogin(){
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
login();
} else if (response.status === 'not_authorized') {
login();
} else {
login();
}
});
}
function login() {
FB.login(function(response) {
if (response.authResponse) {
testAPI();
} else {
// cancelled
}
}, {scope: 'email,user_education_history,user_work_history'}); //permissions
}
function testAPI() {
FB.api('/me', function(response) {
alert('Good to see you, ' + response.email + '.');
});
}
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
And my html button:
<button style="position:absolute; margin-top: 4px;margin-left:600px;" onclick="doLogin();">Sign Up With Facebook</button>
EDIT:
When I check the console in my chrome browser where the button doesnt work, I receive the following message:
Given URL is not allowed by the Application configuration.: One or more of the given URLs is not allowed by the App's settings. It must match the Website URL or Canvas URL, or the domain must be a subdomain of one of the App's domains.
Again, the button works perfectly fine when I am running it in my mozilla browser in my linux OS but not on my other computer. I am still not sure what is causing this difference?? Thanks again all for your help.
I'm trying to enable Facebook login to my site via Javascript but when the user logs in, the Facebook popup doesn't closes. Instead it shows the following message:
An error may have occurred as part of the login process. You can close this window and try returning to the application, though it may ask you to login again. This is likely due to a bug in the application.
If I click again on the login button, everything works fine.
This is my code:
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
// Init the SDK upon load
window.fbAsyncInit = function() {
var doLogin = function (response) {
url = '//'+window.location.hostname+'? access_token='+response.authResponse.accessToken;
window.top.location = url;
};
FB.init({
appId : 'MY_APP_ID', // App ID
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true, // parse XFBML
oauth : true,
channelUrl : '//'+window.location.hostname+'/static/html/channel.html'
});
FB.Event.subscribe('auth.login', function(response) {
doLogin(response);
})
FB.getLoginStatus(function(response) {
if (response.status == 'connected') {
doLogin(response);
}
}, true);
}
You should try something like this:
https://gist.github.com/3370553
Or you could replace AuthorizeFacebook with "FB.login" from the JS API
My App on Facebook uses the JavaScript SDK. When a user navigates to my app page and goes to my app, a popup asks them to authorize the app. This works well.
However, if they authorize the app, then return to it later, another pop-up (which i believe to be another authorization window) will quickly open then close.
What is my code is doing this? Code is below.
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '221953271200525', // App ID
channelURL : '//www.vidjahgames.com/fall/channel.html', // 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
FB.login(function(response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.');
});
} else {
console.log('User cancelled login or did not fully authorize.');
}
}, {scope: 'email'});
};
// 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>
FB.login makes the popup appear, so you should first know if the user is logged in and if not make the login popup appear:
FB.getLoginStatus(function(response) {
if (response.session) {
// A user has logged in, and a new cookie has been saved
FB.api('/me', function(response) {
_userName = response.name;
alert("hello"+_userName);
}.bind(this));
} else {
FB.login(function(response) {
// the rest of your code here...
}
}
});