[英]How to return access token facebook?
I don't know how to sync in api FB.login and here is my code:我不知道如何在 api FB.login 中同步,这是我的代码:
function getAccesToken(){
var access_token = "";
FB.login(function(response) {
if (response.authResponse) {
access_token = FB.getAuthResponse()['accessToken'];
} else {
console.log('User cancelled login or did not fully authorize.');
}
}, { scope: '' });
return access_token;
}
when I call function当我调用函数时
var access_token = getAccessToken();
but it return empty string.但它返回空字符串。 How can I fix it to be synchronous?
我怎样才能修复它是同步的?
You can't make this synchronous - and it would be extremely bad practice if you could.你不能让这个同步 - 如果可以的话,这将是非常糟糕的做法。 Instead, call a function in the
FB.login
callback handler that contains the access token.相反,调用包含访问令牌的
FB.login
回调处理程序中的函数。 Try this:尝试这个:
function getAccesToken() {
FB.login(function(response) {
if (response.authResponse) {
loginComplete(FB.getAuthResponse()['accessToken']);
} else {
console.log('User cancelled login or did not fully authorize.');
}
}, { scope: '' });
}
function loginComplete(token) {
console.log(token);
// do something here, now the user is logged in...
}
FB.login
needs to be called on user interaction (mouse click) and you have to use the callback function of FB.login
to get the Access Token asynchronously - the Access Token will be in the response value. FB.login
需要在用户交互(鼠标点击)时调用,您必须使用FB.login
的回调函数异步获取 Access Token - Access Token 将在响应值中。 For example:例如:
document.getElementById('loginBtn').addEventListener('click', function() {
FB.login(function(response) {
if (response.authResponse) {
doSomethingWithTheToken(response.authResponse.accessToken);
}
}, {scope: 'email,public_profile', return_scopes: true});
}, false);
function doSomethingWithTheToken(token) {
console.log(token);
}
Source (and a lot more information about the JavaScript SDK): http://www.devils-heaven.com/facebook-javascript-sdk-login/来源(以及更多关于 JavaScript SDK 的信息): http : //www.devils-heaven.com/facebook-javascript-sdk-login/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.