Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
33
rated 0 times [  35] [ 2]  / answers: 1 / hits: 128649  / 14 Years ago, fri, january 21, 2011, 12:00:00

I need to get the access token from FB.login method in Javascript SDK. My login code is



FB.login(function(response) {
if (response.session) {
if (response.perms) {

} else {
// user is logged in, but did not grant any permissions
alert(No Permission..);
}
} else {
// user is not logged in
alert(Please login to facebook);
}
}, {perms:'read_stream,publish_stream,offline_access'});


Is there any way to get access token? I am able to get the access token using PHP.


More From » facebook

 Answers
37

You can get access token using FB.getAuthResponse()['accessToken']:





FB.login(function(response) {
if (response.authResponse) {
var access_token = FB.getAuthResponse()['accessToken'];
console.log('Access Token = '+ access_token);
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: ''});


Edit:
Updated to use Oauth 2.0, required since December 2011. Now uses FB.getAuthResponse();
If you are using a browser that does not have a console, (I'm talking to you, Internet Explorer) be sure to comment out the console.log lines or use a log-failsafe script such as:



if (typeof(console) == undefined) { console = {}; } 
if (typeof(console.log) == undefined) { console.log = function() { return 0; } }

[#94109] Wednesday, January 19, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
benitoh

Total Points: 150
Total Questions: 113
Total Answers: 104

Location: India
Member since Wed, Aug 26, 2020
4 Years ago
benitoh questions
Sun, Mar 21, 21, 00:00, 3 Years ago
Mon, May 13, 19, 00:00, 5 Years ago
;