I have seen examples of accessing users data in canvas application with oAuth and graph api. But what I want to do is access user data such as the name, email address and photo within a page tab application. I have looked around and seen no examples. I am wondering if this is possible and how I would access the user data and yes I would like to do it through oAuth(asking them for the data).
Best Regards
It's the same exact way you do it for Canvas Apps:
<?php
$app_id = YOUR_APP_ID;
$canvas_page = YOUR_CANVAS_PAGE_URL;
$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($canvas_page);
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
if (empty($data["user_id"])) {
echo("<script> top.location.href='" . $auth_url . "'</script>");
} else {
echo ("Welcome User: " . $data["user_id"]);
}
?>
If you need to access the user email just add the scope parameter (with the email permission) to your authentication url.
Related
I am creating an app in facebook and when user enters my app then the authentication will happen and after that i will get the access token and id of user now i want the details of the same user and i have to store that in database .... how to do this
CODE I HAVE TRIED IS
<?php
$app_id = "xxxxxxxxxxx";
$canvas_page = "xxxxxxxxxxxxx";
$auth_url = "https://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($canvas_page)."&scope=email,user_birthday";
$signed_request = $_REQUEST["signed_request"];
//print_r($signed_request);
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
//print_r($payload);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
//print_r($data);
if(!isset ($_GET['code'])) {
if (empty($data["user_id"])) {
echo("<script> top.location.href='" . $auth_url . "'</script>");
} else {
$accessToken = $data["oauth_token"];
echo ("<br/><br/>Welcome User: " . $data["user_id"]);
}
} else {
echo("<script> top.location.href='" . $pageUrl . "'</script>");
}
?>
Now i want the username and other details in string or array format ...
Please help me....
Using this Graph api :
https://graph.facebook.com/me?access_token=XXXXXXXXXXXXXXX
You can choose the fields you want returned using the fields query parameter:
https://graph.facebook.com/me?fields=id,name
You can use the Facebook PHP SDK which automatically deal with this situation and sets the access token, or you may manually specify the access token using
setAccessToken($access_token) method
and pass it your user's access token and make Graph API call using api() method to get data about user. If you want to do away with the SDK then you need to add access token as parameter to Graph API end points.
I have been trying to solve this problem for three days now, it's really simple for those who know the open graph api well. I'm new to Facebook integration but have some PHP experience.
Basically all I'm trying to do is retrieve the following information from users and store it in a database.
Facebook User ID:
Name
Gender
Email
I have done the user ID, name and gender by using:
$contents = file_get_contents ('https://graph.facebook.com/'.$user);
$json=json_decode($contents,true);
$userid = $json['id'];
$username = $json['name'];
$usergender = $json['gender'];
$useremail = $json['email'];
This works and I understand I need to ask for permissions to access the email which I have done using this code:
$app_id = "211665122244023";
$canvas_page = "http://apps.facebook.com/midcitymafia/";
$auth_url = "https://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($canvas_page) . "&scope=email,publish_actions";
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
But how do I get the access token and then use it to retrieve email from graph?
##UPDATE, THIS IS MY CURRENT CODE, STILL CAN'T SEEM TO GET IT TO WORK...
require 'src/facebook.php';
$app_id = "211665122244023";
$canvas_page = "http://apps.facebook.com/midcitymafia/";
$auth_url = "https://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($canvas_page) . "&scope=email,publish_actions";
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
if (empty($data["user_id"])) {
echo("<script> top.location.href='" . $auth_url . "'</script>");
} else {
$graph = json_decode(file_get_contents("https://graph.facebook.com/".$user_id . "/?accesstoken=" .$data['access_token']));
}
$userid = $graph->id;
$username = $graph->name;
$usergender = $graph->gender;
$useremail = $graph->email;
?>
<br>
<?php echo 'ID: ' . $userid; ?>
<br>
<?php echo 'Name: ' . $username; ?>
<br>
<?php echo 'Gender: ' . $usergender; ?>
<br>
<?php echo 'Email: ' . $useremail; ?>
signed_request contains user access_token within itself.
In your case access_token is in $data['access_token']
Storing user access_token isn't a best idea since they provided for a short period of time and expired later. To get permanent access_token you need to request offline_access permission from user (I personally wouldn't recommend it since you may achieve most things without requiring offline_access, in many cases Application access_token may fit your needs).
I wrote the following code to authenticate a facebook application but instead of google many days it did not solve.
The problem is the when the application is first accessed user is prompt to application authentication dialog. when the user allows application it keeps blinking the url. neither shows dialog nor application. After closing the browser yes it runs fine. please can you find out what is the problem. I will be very thankful.
Code:
$canvas_page = "---my canvas url----"; //i removed actual url here!
// Create our Application instance (replace this with your appId and secret).
$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($canvas_page) ."&scope=email,read_stream";
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
if (!empty($data["user_id"])) {
echo 'Hello: '.$data["user_id"];
}else{
echo "What is the hell";
echo("<script> location.href='" . $auth_url . "'</script>");
}
It should work
$canvas_page = "---my canvas url----"; //i removed actual url here! // Create our Application instance (replace this with your appId and secret). $auth_url = "http://www.facebook.com/dialog/oauth?client_id=" . $app_id . "&redirect_uri=" . urlencode($canvas_page) ."&scope=email,read_stream"; $signed_request = $_REQUEST["signed_request"]; list($encoded_sig, $payload) = explode('.', $signed_request, 2); $data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true); if (!empty($data["user_id"])) { echo 'Hello: '.$data["user_id"]; }else{ echo "What is the hell"; echo("<script> location.href='" . $auth_url . "'</script>"); }
use:
echo("<script> top.location.href='" . $auth_url . "'</script>");
With top!
I'm using this standard facebook authorization code but it's not showing the count of users who've approved the app. It's still at 0 and I know several people have approved the app. Is it something in my code or what?
$app_id = YOUR_APP_ID;
$canvas_page = YOUR_CANVAS_PAGE_URL;
$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($canvas_page);
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
if (empty($data["user_id"])) {
echo("<script> top.location.href='" . $auth_url . "'</script>");
} else {
echo ("Welcome User: " . $data["user_id"]);
}
Have you replaced those YOUR_APP_ID & YOUR_CANVAS_ID with valid ones or declared them somewhere?
What does this return?
print_r($signed_request);
Are you getting redirected to the login page or not?
Does ANYONE have a working (php) facebook authorize app (in iframe) script. I've tried all the scripts I can find on google none of them work. I just want it to authorize the app and go the the proper page after authorization. With or without a button to start the authorization process.
<?php
$app_id = "[YOUR APP ID]";
$canvas_page = "[YOUR APP URL]";
$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($canvas_page);
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
if (empty($data["user_id"])) {
echo("<script> top.location.href='" . $auth_url . "'</script>");
} else {
echo ("Welcome User: " . $data["user_id"]);
}
?>