如何从 javascript SDK 中的 FB.login 方法获取访问令牌 [英] How to get access token from FB.login method in javascript SDK
问题描述
我需要从 Javascript SDK 中的 FB.login
方法获取访问令牌.我的登录代码是
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'});
有没有办法获得访问令牌?我可以使用 PHP
获取访问令牌.
Is there any way to get access token? I am able to get the access token using PHP
.
推荐答案
您可以使用 FB.getAuthResponse()['accessToken']
获取访问令牌:
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: ''});
更新为使用 Oauth 2.0,自 2011 年 12 月起需要.现在使用 FB.getAuthResponse();
如果您使用的是没有控制台的浏览器,(我在说的是 Internet Explorer)请务必注释掉 console.log
行或使用日志故障安全脚本,例如:
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; } }
这篇关于如何从 javascript SDK 中的 FB.login 方法获取访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!