Reputation: 1241
I have a functional facebook application that use the JS SDK. I can use the JS SDK to get my users facebook ID and access token, and it seem to work fine. However, my JS script need to call some server side PHP code via JQuery AJAX, in order to make some server side related operations... but i still need an access to my user's access token on the PHP code as well. On the official facebook documentation of the PHP SDK, i saw the following:
The Facebook SDK for PHP can work in conjunction with the Facebook SDK for Javascript to provide seamless session management across both the client and server-sides of an app.
To enable this functionality, ensure that when you embed and initialize the JS SDK, you set both the status and the cookie parameters of the object passed to FB.init() to true.
I followed the exact instructions as written above, but it looks like my PHP code fails to get the user's facebook ID and access token after I initialize my facebook PHP object.
What do I miss? i don't understand it, because by what the documentation say, my user's access token should be passed automatically to the PHP SDK using a cookie.
Should it be done in some other way?
Thanks!
Upvotes: 2
Views: 2481
Reputation: 1861
You can still pass the access token obtained from js-SDK to PHP along with your ajax request and use it with the PHP.
<script>
// This is called with the results from from FB.getLoginStatus().
function statusChangeCallback(response) {
console.log(response);
if (response.status === 'connected') {
// Logged into your app and Facebook.
// testAPI();
var accessToken = response.authResponse.accessToken;
console.log(accessToken);
//ajax call to PHP **************************************************************
//*******************************************************************************
$.get("/ajaxpage.php?token="+accessToken+"&uid="+response.authResponse.userID,"",function(data){
// on reply from the server
});
} else if (response.status === 'not_authorized') {
// The person is logged into Facebook, but not your app.
} else {
// The person is not logged into Facebook.
}
}
// This function is called when someone finishes with the Login
// Button. See the onlogin handler attached to it in the sample
// code below.
function checkLoginState() {
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
}
window.fbAsyncInit = function() {
FB.init({
appId: '..................',
cookie: true, // enable cookies to allow the server to access the session
xfbml: true, // parse social plugins on this page
version: 'v2.2' // use version 2.2
});
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
};
// 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'));
<fb:login-button scope="public_profile,email" onlogin="checkLoginState();">
</fb:login-button>
Upvotes: 0
Reputation: 70
I know it's late, but just in case that someone else need to pass the access token from JS SDK to PHP SDK you can Achieve it by calling
$jsHelper = $fb->getJavaScriptHelper();
$signedRequest = $jsHelper->getSignedRequest();
$accessToken = $jsHelper->getAccessToken();
for more detailled information about the getJavaScriptHelper()
visite Facebook\Helpers\FacebookJavaScriptHelper
Note: don't forget to set cookie:true
in the config of FB.init({/*..*/})
Upvotes: 0
Reputation: 44649
If the access_token
is retrieved using the Javascript SDK, then you know you'll only get it after the first page load (on page reload or on ajax call).
Although, be sure to check that your SDKs are setted up correctly (as well as the channel file) as this could create trouble with cross-domain communication - and because cookie are most of the time blocked in an iframe without some kind of hack around (mostly in IE and Safari).
Also, the cookie the JS SDK will setup will be used via PHP SDK functions, for you, it seems like getAccessToken
is what you want. https://developers.facebook.com/docs/reference/php/facebook-getAccessToken/
Upvotes: 0