Facebook Authorize App Script - php

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

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

Facebook Authorization runs in to a redirect loop

I am using the Facebook app canvas code in the documentation and it keeps redirecting after I hit "allow". Not sure what is wrong. Do check if it is a bug or it is just me. Its from
http://developers.facebook.com/docs/appsonfacebook/tutorial/
<?php
$app_id = ""; //Added my app id
$canvas_page = ""; //Added my 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"]);
}
?>
my recommend is using php Sdk it makes every thing easier for you & the code for login will be like this :
<?php
include_once('src/facebook.php');
$config = array(
'appId' => 'xxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxx',
'fileUpload' => FALSE, // optional
);
$canvas_page = ""; //Added my canvas page
$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($canvas_page);
$uid=$facebook->getuser();
if(empty($uid)){ echo("<script> top.location.href='" . $auth_url . "'</script>");}
else { echo ("Welcome User: " . $uid);}

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 application authentication issue

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!

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?

Categories