如何在 PHP 中获取 Facebook 访问令牌,同时在 JS 中使用 oauth2?
我在网站上使用 FB 连接和 Facebook 的登录按钮。
我使用它登录,然后在登录时重新加载页面,我尝试在 PHP 中获取具有 access_token 的 cookie,但未能成功。以下是我的js代码:
window.fbAsyncInit = function() {
FB.init({
appId : 'xxxxxxxx',
status : true,
cookie : true,
xfbml : true,
oauth : true
});
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
});
};
(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));
Facebook登录按钮代码:
<div class="fb-login-button" scope="email,user_checkins" >Login with Facebook</div>
现在,当用户使用facebook登录时,我尝试使用access_token获取其数据,但没有从cookie中获取access_token。 我正在使用以下代码:
$cookie = $this->get_facebook_cookie(APP_ID, APP_SECRET);
echo $cookie['access_token'];
if(isset($cookie['access_token'])){
$user = json_decode(@file_get_contents('https://graph.facebook.com/me?access_token=' .$cookie['access_token']));
echo $user->id;
if(isset($user->id)){
// my stuff
}
}
我怀疑它甚至没有获取 cookie。当它在客户端登录时。那么如何在 PHP 中获取数据呢?还需要在 PHP 端再次重复整个登录过程吗?我该如何处理?
I am using FB connect and login button of facebook on a site.
I logged in using it and then on login, I reload the page, I tried to get the cookie having access_token in PHP but failed to do so. Following is my js code:
window.fbAsyncInit = function() {
FB.init({
appId : 'xxxxxxxx',
status : true,
cookie : true,
xfbml : true,
oauth : true
});
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
});
};
(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));
Fb login button code:
<div class="fb-login-button" scope="email,user_checkins" >Login with Facebook</div>
Now I when user login using facebook, I try to get its data using access_token, but not getting that access_token from cookies.
I am using following code:
$cookie = $this->get_facebook_cookie(APP_ID, APP_SECRET);
echo $cookie['access_token'];
if(isset($cookie['access_token'])){
$user = json_decode(@file_get_contents('https://graph.facebook.com/me?access_token=' .$cookie['access_token']));
echo $user->id;
if(isset($user->id)){
// my stuff
}
}
I doubt it is not even getting cookie. While it logged at client side. So how can I get data in PHP then? need to again repeat whole login process on PHP side too? How do I deal with it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不直接使用 Facebook php sdk 来自动加载 js sdk 设置的 cookie? https://github.com/facebook/facebook-php-sdk
Why not just use the Facebook php sdk which automatically loads the cookie set by the js sdk? https://github.com/facebook/facebook-php-sdk