Reputation: 722
i really need some help over this one..
i m trying like over here Facebook PHP SDK $facebook->getSignedRequest() Get User ID and Name. (Page Tab) to create an app that is linked with a facebook page..so far so good.. the problem is that i cannot retrieve user id, no matter what, using php-sdk..
i ve tried also this fix, https://www.webniraj.com/2012/12/19/facebook-php-sdk-fixing-getuser-on-php-5-4-x/ but it didn't make any difference
require 'php-sdk/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'myappid',
'secret' => 'mysecretid',
));
$theRequest = $facebook->getSignedRequest();
$user = $facebook->getUser();
var_dump($user);
no matter what i ve tried i haven't been able to retrieve the user id..
i tried the link Paul suggested
with the following code
if ($_REQUEST) {
$signed_request = $_REQUEST['signed_request'];
} else {
echo '$_REQUEST is empty';
}
function parse_signed_request($signed_request) {
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
// decode the data
$sig = base64_url_decode($encoded_sig);
$data = json_decode(base64_url_decode($payload), true);
return $data;
}
function base64_url_decode($input) {
return base64_decode(strtr($input, '-_', '+/'));
}
$data = parse_signed_request($signed_request);
var_dump($data);
i got a "NULL" as response.. :S
second edit.. i got this code
$information=parse_signed_request($signed_request, $secret); $oauth_token=$information["oauth_token"];
var_dump($information); var_dump($oauth_token);
$app_id = "myappid";
$canvas_page = "https://apps.facebook.com/app_namespace";
$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($canvas_page) . "&scope=read_stream, friends_likes";
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
if (empty($oauth_token)) {echo("<script> top.location.href='" . $auth_url . "'</script>");}
in this way, i manage to get auth dialog, but in the redirect i get a message of misconfigured app..if i reenter the app, then i access all necessary info..token etc.. so if i find a solution why on the redirect i get misconfigured app, i guess i sort of solved my problem?
Upvotes: 0
Views: 3400
Reputation: 722
i don't know who may find usefull this or not.. i think it would be a good idea, if facebook, suggested the discrete instances where each sdk is better applicable.. after spending quite few hours on the php sdk integrating with facebook app on a page tab, so, the app on facebook was out of question, i ended up that the javascritp sdk would serve my purpose better, since, a) it doesn't redirect the user, from my page to authenticate b) it doesn't require app to be existing as a facebook app as well, (in my case i wanted to be shown as a page tab) c) it make a kind of overlay of all the content, bringing forward the auth dialog, and then returning to my page, roviding all usefull information..
copying the code from several fb sources this worked for me
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// the user is logged in and has authenticated your
// app, and response.authResponse supplies
// the user's ID, a valid access token, a signed
// request, and the time the access token
// and signed request each expire
uid = response.authResponse.userID;
//alert(uid);
var accessToken = response.authResponse.accessToken;
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
FB.login(function(response) {
if (response.authResponse) {
uid = response.authResponse.userID;
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
});
} else {
}
});
} else {
// the user isn't logged in to Facebook.
}
});
// Additional initialization code such as adding Event Listeners goes here
};
Upvotes: 1
Reputation: 4466
First of all, signed_request doesn't give the user_id
and oauth_token
until and unless the user has logged into your app.It would just give basic information about the User within the user
key which is a JSON object containing the locale string, country string and the age object.
So if you want to get the user_id
implement Login or for test case here you can do following
<?php
$params = array(
'redirect_uri' => 'http://yourdomain.com/post_login_page'
);
$loginUrl = $facebook->getLoginUrl($params);
if(!$facebook->getUser()):
?>
<script>window.location.href = "<?php $loginUrl; ?>"</script>
<?php endif ?>
Upvotes: 0