2
我在網站上使用FB連接和Facebook登錄按鈕。在JS中使用oauth2時,如何在PHP中獲得Facebook訪問令牌?
我登錄使用它,然後登錄,我重新加載頁面,我試圖讓PHP有access_token在PHP中但未能這樣做。以下是我的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));
Fb的登錄按鈕代碼:
<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
}
}
我懷疑它甚至沒有得到餅乾。它在客戶端登錄。那麼我怎麼才能在PHP中獲取數據呢?需要再次在PHP端重複整個登錄過程?我如何處理它?
上面是當用戶登錄和彈出窗口打開時的fb連接方案,因此在這種情況下,我使用的是JS。 – Hafiz 2012-11-12 10:13:31