Storing Access Token - php

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).

Related

facebook app authorizing: An error occurred. Please try again later

I’ve just created a Facebook app, this is my first attempt and I followed Facebook developer’s documentation totally, while completing everything as stated I am stucked at this error while authorizing app:
An error occurred. Please try again later.
<?php
$app_id = '1603369454518730';
$app_secret = '511b194f6sdgg6eca7cc748d7be6d82d';
//$canvas_page = "http://apps.facebook.com/myapp";
$canvas_page = "http://myappweb.com/app/landhere.php";
$auth_url = "https://www.facebook.com/dialog/oauth?client_id=" . $app_id . "&redirect_uri= " . urlencode($canvas_page);
//Requesting Signed Parameter:
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
//User Info. Variables:
try {
$userId = $data["user_id"];
} catch(Exception $e) {
echo $e -> getMessage();
echo "<br>";
}
if (!empty($data["user_id"])) {
if ($data['page']['liked']) {
echo "hello";
} else {
echo "like page";
}
} else {
echo("<script> top.location.href='" . $auth_url . "'</script>");
}
?>
Ive tried suggestion that I found in other threads such as checking app id and app secret, disabling sandbox. But none of this has worked yet for me. Kindly help me with this.
Thank you.
I'm not into coding too much, but kinda sorted it out.
Your canvas page should be the url of the page not your app url.
Also I tried using the following code and it works.
<?php
$app_id = "APP_ID";
$canvas_page = "CANVAS_PAGE";
$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 I'm not mistaken, (My interaction with the PHP SDK is minimal), you need to add a redirection url to which FB will return when authentication is complete, to acomplish that you will have to do two steps:
1) $auth_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($canvas_page);
2) In you'r applications' page: https://developers.facebook.com/apps/app_id
press Edit and in "Select how your app integrates with Facebook"
select Website.
put in the URL the you'r http://apps.facebook.com/myapp.
good luck

Trying to use oauth with facebook problems

Im trying to use this code to start building a simple facebook app but I cant seem to get to grips with the access token part so i can get the users birthday etc.
Can someone please take a look and let me know what im doing wrong :
<?php
$app_id = "*********";
$canvas_page = "https://apps.facebook.com/hotness-battle/";
$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"];
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 {
$token_url = 'https://graph.facebook.com/oauth/access_token?client_id=200482573356726&redirect_uri=http://www.impact25.com/hotness-battle/&client_secret=*******&code='.$data['oauth_token'].'';
echo("<script> top.location.href='" . $token_url . "'</script>");
$uid = $data["user_id"];
$token = $data['oauth_token'];
$full_name = json_decode(file_get_contents('http://graph.facebook.com/'.$uid))->name;
$gender = json_decode(file_get_contents('http://graph.facebook.com/'.$uid))->gender;
$birthday = json_decode(file_get_contents('http://graph.facebook.com/'.$uid.'?access_token='.$token))->birthday;
echo $full_name;
echo '<br><br>';
echo $gender;
echo '<br><br>';
echo $token;
echo '<br><br>';
echo $cookie['access_token'];
}
Okay, obviously you just copied the above code from somewhere...here are a couple of tips:
Read the Canvas Tutorial
The second OAuth request is not needed ($token_url) since if the user authorized your app you'll have the access_token in the signed_request
Don't do multiple graph calls, one call will retrieve everything you need
Don't print the access_token to the user
Make secure calls to the graph ( https )
Here is a working code to get you started:
<?php
$app_id = "APP_ID";
$canvas_page = "https://apps.facebook.com/appnamespace";
$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"];
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 {
$uid = $data["user_id"];
$token = $data['oauth_token'];
$graph_url = 'https://graph.facebook.com/' . $uid . '?access_token=' . $token;
$user_info = json_decode(file_get_contents($graph_url));
$full_name = $user_info->name;
$gender = $user_info->gender;
$birthday = $user_info->birthday;
echo $full_name;
echo '<br><br>';
echo $gender;
echo '<br><br>';
echo $birthday;
echo '<br><br>';
}

Accessing a user details from a page application

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.

Facebook Won't Count App Users

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?

Facebook Authorize App Script

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"]);
}
?>

Categories