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