Requesting facebook permissions on tab page - facebook

I have a simple task to do but i am unable to find a solution for my problem.
I have a facebook page tab. Now i want to get the email permission of a user accessing my app and then display his email address on the page tab directly.
I found a solution in the facebook documentation that gets the permission an then redirects to a given url. That works fine. But how can i redirect back to my page tab? if i try to enter the page tab url it just goes to an indefinite loop. (because the $code variable is not set it seems)
<?php
$app_id = "APP_ID";
$app_secret = "APP_SECRET";
$my_url = "REDIRECT_URL";
session_start();
$code = $_REQUEST["code"];
if(empty($code)) {
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url) . "&scope=email" . "&state="
. $_SESSION['state'];
echo("<script> top.location.href='" . $dialog_url . "'</script>");
}
if($_REQUEST['state'] == $_SESSION['state']) {
$token_url = "https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret . "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$graph_url = "https://graph.facebook.com/me?access_token="
. $params['access_token'];
$user = json_decode(file_get_contents($graph_url));
echo("Mail: " . $user->email);
}
else {
echo("The state does not match. You may be a victim of CSRF.");
}
?>

Use the JavaScript SDK as it is all client-side and you will have easier control over where and when things happen. The login dialog can be a modal FB.login() and then call the FB.api() to get the user's email. See: http://developers.facebook.com/docs/reference/javascript/

If you want to do it server side I suggest you use the facebook php sdk at https://developers.facebook.com/docs/reference/php/
it makes things a lot easier. hth.

Related

Facebook fql not showing the photos and albums query

I am using php-sdk for showing facebook information of the user. I have successfully shown the user profile info but while fetching any album or photos I am getting the blank data to me. I read the photos requires access_token.
Also if I put this query in the facebook graph api explorer it showing me the perfect result. So query is right may be my way of passing the url is wrong.
I am not getting the issue.
Please help.
';
$app_secret = '';
$my_url = '';
$config = array(
'appId' => '<appid>',
'secret' => '<appsecrete>',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$code = $_REQUEST["code"];
if($user_id) {
//auth user
if(empty($code)) {
$dialog_url = 'https://www.facebook.com/dialog/oauth?client_id='
. $app_id . '&redirect_uri=' . urlencode($my_url) ;
echo("<script>top.location.href='" . $dialog_url . "'</script>");
}
//get user access_token
$token_url = 'https://graph.facebook.com/oauth/access_token?client_id='
. $app_id . '&redirect_uri=' . urlencode($my_url)
. '&client_secret=' . $app_secret
. '&code=' . $code;
$access_token = file_get_contents($token_url);
$fql_query_url = 'https://graph.facebook.com/'.'fql?q=SELECT+pid,src_small+FROM+photo+WHERE+aid+IN+(SELECT+aid+FROM+album+WHERE+owner=+me())&'.$access_token;
$fql_query_result = file_get_contents($fql_query_url);
$fql_query_obj = json_decode($fql_query_result, true);
//display results of fql query
echo '<pre>';
print_r("query results:");
print_r($fql_query_obj);
echo '</pre>';
}
?>
One parameter needs to be add for $dialog_url variable which is 'scope' which defines permissions to be given for application
$dialog_url = 'https://www.facebook.com/dialog/oauth/?client_id='.$app_id.'&redirect_uri='.urlencode($my_url).'&scope=user_photos';
echo("<script>top.location.href='" . $dialog_url . "'</script>");
for reference,
http://developers.facebook.com/docs/reference/dialogs/oauth/
Thanks
Shreyas

FQL - getting access token

I need someone help.
When I open https://developers.facebook.com/docs/reference/api/ and clicked https://graph.facebook.com/me/likes?access_token=blablabla, I got all page that I've liked.
My questions was, from where I got the access_token value? can I get it by script?
Facebook developer site leverages an internal test console application that all developers can use to test calls within the documentation, site automatically appends the access_token.
A next place one can find the access token is https://developers.facebook.com/tools/explorer with the Graph API Explorer app.
You can also get it via script by following one of the authentication flows described at http://developers.facebook.com/docs/authentication/
You will need to create an application at https://developers.facebook.com/apps then,
For example via PHP using a server side flow,
Redirect the user to the OAuth Dialog ($dialog_url shown below)
A code will be given in response
Exchange the code for a user access token ($token_url shown below)
<?php
$app_id = "YOUR_APP_ID";
$app_secret = "YOUR_APP_SECRET";
$my_url = "YOUR_URL";
session_start();
$code = $_REQUEST["code"];
if(empty($code)) {
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
. $_SESSION['state'];
echo("<script> top.location.href='" . $dialog_url . "'</script>");
}
if($_SESSION['state'] && ($_SESSION['state'] === $_REQUEST['state'])) {
$token_url = "https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret . "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$graph_url = "https://graph.facebook.com/me/likes?access_token="
. $params['access_token'];
$likes = json_decode(file_get_contents($graph_url));
print_r($likes); // A dump of the likes data
}
else {
echo("The state does not match. You may be a victim of CSRF.");
}
?>
See more of the reference information available at http://developers.facebook.com/docs/authentication/server-side/

How can I send facebook application requests using a customized popup?

I want a customized popup from which i want to send application request ?
How do i achieve this ?
you can send app request via http graph api , that is just an http post ... so you can build your own interface for that see https://developers.facebook.com/docs/channels/
<?php
$app_id = YOUR_APP_ID;
$app_secret = YOUR_APP_SECRET;
$token_url = "https://graph.facebook.com/oauth/access_token?" .
"client_id=" . $app_id .
"&client_secret=" . $app_secret .
"&grant_type=client_credentials";
$app_access_token = file_get_contents($token_url);
$user_id = THE_CURRENT_USER_ID;
$apprequest_url ="https://graph.facebook.com/" .
$user_id .
"/apprequests?message='INSERT_UT8_STRING_MSG'" .
"&data='INSERT_STRING_DATA'&" .
$app_access_token . "&method=post";
$result = file_get_contents($apprequest_url);
echo("App Request sent?", $result);
?>

How to test If I am logged into facebook from out of it?

I have this facebook login code
$app_id = "xxxxxxxxxxxxxx";
$app_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$my_url = "http://xxxxxxxxxxxxx";
$code = $_REQUEST["code"];
if(empty($code)) {
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
$dialog_url = "https://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
. $_SESSION['state'];
echo("<script> top.location.href='" . $dialog_url . "'</script>");
print_r( $dialog_url);
die();
}
if($_REQUEST['state'] == $_SESSION['state']) {
$token_url = "https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret . "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$graph_url = "https://graph.facebook.com/me?access_token="
. $params['access_token'];
$user = json_decode(file_get_contents($graph_url));
echo("Hello " . $user->name ." Create Event");
}
else {
echo("The state does not match. You may be a victim of CSRF.");
}
can I test if I am already connected to facebook website, consider it login to this page ? and no login link is being displayed ?
The question is pretty old but the answer might be helpful to others who finds similar situation. I guess, what you mean to say is to check if you code has successfully logged you to ur facebook account.
Security settings in facebook now has a feature to watch active sessions, gives you the information of when and from where have you logged in to your account. It is under:
Account Settings > Security > Active Sessions
What you can do is logout of all the sessions with end activity & finally logout of current session, then run the code. Now login via browser (or a different device, but login via browser or a different browser works fine) to see if you have got any more active sessions.

Errors in chrome/ ie redirecting to facebook app from fan page

I'm a little confused here. The basic problem is on my fan page when I click on the icon for the app I made it works fine in firefox, but in IE I get the error "This content cannot be displayed in a frame" and in chrome it just never opens the app and goes blank. This has happened to me on the facebook profile I created the app with as well as a test account. The weird thing is that on my other facebook account it works perfectly fine on all browsers. The app also works when directly gone to from search or recent apps. I recently changed the name of the app and security id and reconnected it to my page. Under app settings my tab urls are correct. The code at the top of my canvas page consists of:
<?php
$app_id = "181247898619054";
$app_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";
$my_url = "https://apps.facebook.com/wellnessq/";
session_register();
session_start();
if (!isset($_REQUEST["code"]))
{
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url) . "&scope=email&state="
. $_SESSION['state'];
echo("<script> top.location.href='" . $dialog_url . "'</script>");
exit;
}
$code = $_REQUEST['code'];
{
$token_url = "https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret . "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$graph_url = "https://graph.facebook.com/me?access_token="
. $params['access_token'];
$user = json_decode(file_get_contents($graph_url));
}
?>
Any ideas? If you want to see the errors yourself just let me know and I'll post where to find them. Thanks.
Can you use a PHP header to redirect to your link?
header( 'Location: http://www.yoursite.com/new_page.html' ) ;