How do I get Facebook accessToken after login? - javascript

I can login but I need to get the accessToken, so that other APIs like get friends etc. can be invoked.
The method, getLoginStatus, gets called but doesn't get into the success block on response.
Here is my code:
$(document).ready(function() {
window.fbAsyncInit = function() {
FB.init({
appId: <<My appId>>, // 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
});
FB.getLoginStatus(function(response){
alert('response='+response);
});
window.setTimeout(checkLogStatus, 1000);
function checkLogStatus(){
alert("check");
// fetch the status on load
FB.getLoginStatus(function(response){
alert('response='+response);
});
}
};
// 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));
});

I need to get the accessToken
The SDK is usually able to handle the access token on its own pretty well – including it in any following requests to the API automatically, without any need for you to do something manually.
So it’s not really clear to me, what problem you are actually having/trying to solve here.

Related

Facebook Login Error (Javascript)

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

Facebook user email id using javascript SDK

I am currently using the following code to get the email id of the user using my app
function get_id(cb){
FB.init({
appId : .......,
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.login(function(response) {
if (response.authResponse) {
var token = response.authResponse.accessToken;
alert(token);
accessToken = token;
FB.api('/me?access_token=' + token + '', function (response) {
cb(response.email);
});
}
},{scope: 'email'});
(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));
}; // ending of get_id()
get_id(function (email) {
userID=email;
alert(email);
});
the current code is in a separate javascript file and this function is called when a button is pressed , I have also included <script src="http://connect.facebook.net/en_US/all.js"></script> in the HTML file . my browser (google chrome) gives me the following error each time I try and access it
"Uncaught ReferenceError: FB is not defined "
I am relatively new to developing Facebook apps , so all the reference I have is the developers page at facebook , I am sure it is a small error but I just cant wrap my head around it , appreciate it if anybody could point out where I could be going wrong
Your code will not load correctly. This part:
(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));
Should not be inside any function, put this in a script tag, after the body opening. It can't be in a separate file. This only does a async-load of all.js, that is the Facebook's JS SDK. If you are using the async-load, you should not put <script src="http://connect.facebook.net/en_US/all.js"></script> in your document, because this will do a sync-load.
This other part of your code:
FB.init({
appId : .......,
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
Also should not be inside the function. This fragment is responsible for starting the FB JS SDK in your page, with your app information, it only have to be called one time for each page, otherwise you will be starting the engine multiple times and it will have a caotic behavior. The "FB" object will only be created after the FB JS SDK loads. You don't know when it's going to happen, because of the async load. So you must assign the FB.init to this window event:
window.fbAsyncInit = function() {
FB.init({
appId : .......,
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
};
You want to create a get_id function that uses the Facebook information. You have to be careful with that. With you are sure that this function will be called after the complete load of FB JS SDK, you can create it in any part of your page. BUT I think the most safe way is to create it inside the window.fbAsyncInit. Don't worry about the token, the SDK does it for you:
window.fbAsyncInit = function() {
FB.init({
appId : .......,
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
function get_id(cb){
FB.login(function(response) {
if (response.authResponse) {
FB.api('/me', function (response) {
cb(response.email);
});
}
},{scope: 'email'});
}
};
But when you do that, you will be not sure if your function was already created or not. So when you call it, you will have to test for it's existence:
if(get_id && typeof get_id == 'function') {
// It's safe to call your function, go ahead
get_id(function (email) {
userID=email;
alert(email);
});
} else {
// Your function does not exists yet, do other stuff
}
Good luck!
You need to put:
(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));
out of get_id function. So it was called before you press the button
PS: you don't need to pass ?access_token= manually - FB JS SDK does that for you

facebook FB.Event.subscribe('auth.authResponseChange') not working

For the life of me I cant get Event.subscribe('auth.authResponseChange') to work. See code below
<div id="fb-root">
</div>
<script>
window.fbAsyncInit = function () {
FB.init({
appId: 'XXXXXXXXXXXXXXX', // App ID
channelUrl: 'http://XXXXXXXX.us/', // Channel File
status: true, // check login status
cookie: true, // enable cookies to allow the server to access the session
xfbml: true // parse XFBML
});
FB.Event.subscribe('auth.authResponseChange', function (response) {
alert('The status of the session is: ' + response.status);
});
};
// 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));
</script>
<div class="fb-login-button" data-size="xlarge" scope="email" data-show-faces="false"
data-width="400" data-max-rows="1" autologoutlink="true">
Login with Facebook</div>
The login button renders. and i can logout too. only problem is that the auth.response change is not firing and i am not getting the "message" popup
Your code looks fine.
I would access your Facebook app and check on 2 settings in particular.
Check 'Sandbox Mode' -ensure it's set to 'Disabled'
Check your Site URL and/or Canvas URL -ensure they are set to your domain. (i.e. if you are working on this locally your url should look something like http://localhost:#####/ where ##### is your port.
Hope that helps.
Your fbAsyncInit function is called asynchronously so you won't see any alert messages. Try console.log instead for debugging.

facebook log-in API calls timing

Couldn't find similar enough question so I'll ask.
I'm trying to use facebook log-in on my page.
I did everything according the the developer's guides.
However, I'm not sure when I can start calling FB.API calls. when is the user authenticated? how can I know when this operation was done?
<script>
window.fbAsyncInit = function () {
FB.init({
appId: 'someID', // App ID
channelUrl: '//www.domain.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
});
};
// 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>
now I want to call this
FB.api('/me', function (response) {
facebookUser = response;
alert('Your name is ' + response.name);
});
but not sure when is the write time. I get undefined wherever I put it :/
You will want to run the FB.getLoginStatus() (https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/) before you send the user to log in using FB.login() or if the user is logged in, to call the FB.api() method.

How to load facebook JS SDk in the jQuery block?

According to Facebook: "The best place to put this code is right after the opening tag"
Example:
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'YOUR_APP_ID', // App ID
channelURL : '//WWW.YOUR_DOMAIN.COM/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
};
// 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>
But I would like like to add this in the jquery block $(document).ready(function() { }
How can that be done?
Include jQuery, and then...
$(document).ready(function(){
callFB();
loginClick(); // Call function
});
function callFB(){
window.fbAsyncInit = function() {
FB.init({
appId : 'YOUR_APP_ID', // App ID
channelURL : '//WWW.YOUR_DOMAIN.COM/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
};
// 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));
}
function loginClick(){
// your click handling code in here
}
This is more what you're looking for I think...
https://developers.facebook.com/docs/javascript/howto/jquery/
You can try this jQuery plugin i made too.. :]
jQuery.fbInit = function(app_id) {
window.fbAsyncInit = function() {
FB.init({
appId : 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
});
};
// 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));
$('<div />').attr('id','fb-root').appendTo('body');
};
$(document).ready(function(){
$.fbInit('12345678901234');
});

Categories

Resources