如何从JavaScript SDK中的FB.login方法获取访问令牌

我需要从Javascript SDK中的FB.login方法获取访问令牌。 我的login代码是

 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'}); 

有没有办法获得访问令牌? 我能够使用PHP获取访问令牌。

提前致谢….

您可以使用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: ''}); 

编辑:更新为使用Oauth 2.0,自2011年12月以来需要。现在使用FB.getAuthResponse(); 如果您使用的是没有控制台的浏览器(我正在与您通话,请使用Internet Explorer),请确保将console.log行注释掉或使用以下日志安全脚本:

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

response.session.access_token在我的代码中不起作用。 但是这个工作: response.authResponse.accessToken

  FB.login(function(response) { alert(response.authResponse.accessToken); }, {perms:'read_stream,publish_stream,offline_access'}); 

如果您已经连接 ,只需在javascript控制台中input:

 FB.getAuthResponse()['accessToken'] 

https://developers.facebook.com/docs/facebook-login/login-flow-for-web/

 { status: 'connected', authResponse: { accessToken: '...', expiresIn:'...', signedRequest:'...', userID:'...' } } FB.login(function(response) { if (response.authResponse) { // The person logged into your app } else { // The person cancelled the login dialog } }); 

response.session不起作用,因为response.authResponse是oauth迁移后访问响应内容的新方法。
查看详细信息: SDK&Tools> JavaScript SDK> FB.login

 window.fbAsyncInit = function () { FB.init({ appId: 'Your-appId', cookie: false, // enable cookies to allow the server to access // the session xfbml: true, // parse social plugins on this page version: 'v2.0' // use version 2.0 }); }; // Load the SDK asynchronously (function (d, s, id) { var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) return; js = d.createElement(s); js.id = id; js.src = "//connect.facebook.net/en_US/sdk.js"; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk')); function fb_login() { FB.login(function (response) { if (response.authResponse) { console.log('Welcome! Fetching your information.... '); //console.log(response); // dump complete info access_token = response.authResponse.accessToken; //get access token user_id = response.authResponse.userID; //get FB UID FB.api('/me', function (response) { var email = response.email; var name = response.name; window.location = 'http://localhost:12962/Account/FacebookLogin/' + email + '/' + name; // used in my mvc3 controller for //AuthenticationFormsAuthentication.SetAuthCookie(email, true); }); } else { //user hit cancel button console.log('User cancelled login or did not fully authorize.'); } }, { scope: 'email' }); } 
 <!-- custom image --> <a href="#" onclick="fb_login();"><img src="/Public/assetshttp://img.dovov.comfacebook/facebook_connect_button.png" /></a> <!-- Facebook button --> <fb:login-button scope="public_profile,email" onlogin="fb_login();"> </fb:login-button> 
 window.fbAsyncInit = function () { FB.init({ appId: 'Your-appId', cookie: false, // enable cookies to allow the server to access // the session xfbml: true, // parse social plugins on this page version: 'v2.0' // use version 2.0 }); }; // Load the SDK asynchronously (function (d, s, id) { var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) return; js = d.createElement(s); js.id = id; js.src = "//connect.facebook.net/en_US/sdk.js"; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk')); function fb_login() { FB.login(function (response) { if (response.authResponse) { console.log('Welcome! Fetching your information.... '); //console.log(response); // dump complete info access_token = response.authResponse.accessToken; //get access token user_id = response.authResponse.userID; //get FB UID FB.api('/me', function (response) { var email = response.email; var name = response.name; window.location = 'http://localhost:12962/Account/FacebookLogin/' + email + '/' + name; // used in my mvc3 controller for //AuthenticationFormsAuthentication.SetAuthCookie(email, true); }); } else { //user hit cancel button console.log('User cancelled login or did not fully authorize.'); } }, { scope: 'email' }); } 
 <!-- custom image --> <a href="#" onclick="fb_login();"><img src="/Public/assetshttp://img.dovov.comfacebook/facebook_connect_button.png" /></a> <!-- Facebook button --> <fb:login-button scope="public_profile,email" onlogin="fb_login();"> </fb:login-button>