i am having the next problem migrating my old facebook app to Oauth 2.0:
when i try to read the user profile (in php code) i get the error: "OAuthException: An active access token must be used to query information about the current user."
The application makes the login operation via javascript and then it redirects to a page where from php i get the user profile.
The javascript code:
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo $facebook->getAppId() ?>',
cookie : true, // enable cookies to allow the server to access the session
xfbml : true, // parse XFBML
oauth : true // enable oauth
});
// whenever the user logs in, we refresh the page
FB.Event.subscribe('auth.login', function(response) {
window.location="index.php";
});
};
(function() {
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
function login () {
FB.login(function(response) {
if (response.authResponse) {
//console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
//alert('Good to see you, ' + response.name + '.');
var url="./code.php";
window.location=url;
});
} else {
alert("Debes de identificarte y aceptar las condiciones para obtener el descuento");
}
}, {scope: 'email'});
};
</script>
The php part (in code.php):
require 'facebook-php-sdk/src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
));
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$me = $facebook->api('/me');
.....
I have read different posts and tried things like getting the access_token from the javascript login response or from the cookie and then make the call to api('me/?access_token=…), but it didnĀ“t work.
Edit: Thanks a lot to everyone, it's working now!!
Thanks
Best Answer
@moguzalp @brandwaffle @yauros,To get signed_request on non canvas app. i used the follwing code and it works for me. If you are using Oauth 2.0 than u will get cookie named fbsr_app_id.This cookie is nothing but a signed_request.
You can get this cookie and parse the signed_request to get user id and access_token as follows: