Facebook API Error Code: 191 - facebook

I would really appreciate some help. I've searched around, and I feel like I've tried everything but to no avail.
API Error Code: 191
API Error Description: The specified URL is not owned by the application
Error Message: Invalid redirect_uri: Given URL is not allowed by the Application configuration.
These are my settings:
App Domains: localhost
Canvas URL: http://localhost/friendsmash/
Secure Canvas URL: *empty*
App Available to Public: No
Valid OAuth redirect URIs: http://localhost/friendsmash/
Edit:
I get the error when I try to go to the canvas URL. In other words, I put:
http://localhost/friendsmash/
in my browser. It redirects me here: https://www.facebook.com/dialog/oauth?client_id=592670390826338&redirect_uri=https%3A%2F%2Fapps.facebook.com%2Ftestappertest%2F&state=8712a908d02c6b6fc3fa9c19c9747296&scope=email%2Cpublish_actions
Then, I see the error. When I go to the canvas page instead, I get a blank page. As for the code, I am following this tutorial: https://developers.facebook.com/docs/games/canvas/canvas-tutorial
I censored the app id and secret. Anyway, this is the relevant part of the code:
<?php
require 'server/fb-php-sdk/facebook.php';
$app_id = 'XXXXXX';
$app_secret = 'XXXXXX';
$app_namespace = 'testappertest';
$app_url = 'https://apps.facebook.com/' . $app_namespace . '/';
$scope = 'email,publish_actions';
// Init the Facebook SDK
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
// Get the current user
$user = $facebook->getUser();
// If the user has not installed the app, redirect them to the Login Dialog
if (!$user) {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => $scope,
'redirect_uri' => $app_url,
));
print('<script> top.location.href=\'' . $loginUrl . '\'</script>');
}
?>

Related

Facebook PHP SDK: getAccessToken not correct on refresh

On my facebook app, I'm logging a user in using the Javascript SDK.
If OK, I redirect him to this PHP page (designed to illustrate my problem):
test.php:
<?php
include('config.php');
require('phpsdk.php');
$access_token = $facebook->getAccessToken();
echo $access_token;
echo "<br /><br />";
$user = $facebook->getUser();
echo $user;
?>
(phpsdk.php handels the PHP SDK initiation)
Accessing test.php the FIRST time, returns a valid access token and the ID of the logged user.
However, when I refresh the page, the access token becomes of the type appID|appSecret.
The user is still logged in: getUser still returns the correct user ID.
Anyone knows what's causing this?
Clearing the Facebook Session with php sdk in redirect when user logs out.
Logout.php redirect user and use sdk to clear session data.
example redirect url. yourdomain.com/Logout.php?destroy=true
refer to: https://developers.facebook.com/docs/reference/php/facebook-destroySession/
<?php
session_start();
require './src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'appID',
'secret' => 'appSecret',
'cookie' => true, // enable optional cookie support
));
if(isset($_GET['destroy'])){
$facebook->destroySession();
}
?>
It is not recommended to call getAccessToken(); over and over the way you described.
You should try first to see if the token is in the browser as a session and if not then try to use the SDK to find or request it.
Check browser for a user, if no user call sdk for user, if still null or 0 auth or reauth.
If user, check browser for access_token, if no token call sdk for token.
<?php
session_start();
include('config.php');
require('phpsdk.php');
// need to replace Your_App_ID and YourAppId with your applications id.
$facebook = new Facebook(array(
'appId' => 'Your_App_ID',
'secret' => 'Your_App_Secret',
'cookie' => true, // enable optional cookie support
));
if(isset($_SESSION['fb_YourAppId_user_id'])){
$user = $_SESSION['fb_YourAppId_user_id'];
}else{
$user = $facebook->getUser();
}
if($user){
if(isset($_SESSION['fb_YourAppId_access_token'])){
$access_token = $_SESSION['fb_YourAppId_access_token'];
}else{
$access_token = $facebook->getAccessToken();
}
}
echo $user;
echo "<br /><br />";
echo $access_token;
?>

facebook oauth url problems

I have the following code, which are working partly
<?php
require_once 'facebook.php';
$app_id = 'MY_APP_ID';
$app_secret = 'MY_APP_SECRET';
$app_url = 'http://mysite.com/index.php/';
$scope = 'user_about_me';
// Init the Facebook SDK
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
// Get the current user
$user = $facebook->getUser();
// If the user has not installed the app, redirect them to the Auth Dialog
if (!$user) {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => $scope,
'redirect_uri' => $app_url,
));
print('<script> top.location.href=\'' . $loginUrl . '\'</script>');
}
$user_profile = $facebook->api('/me');
echo "id: " . $user_profile['id'];
?>
I get the correct id printed out on this page, but on several other pages i go into the if(!$user).
But there is an error in the oauth url:
API Error Code: 191
API Error Description: The specified URL is not owned by the application
Error Message: Invalid redirect_uri: Given URL is not allowed by the Application configuration.
I have found similar problems here on stackoverflow, but can't seem to fix it :/
Under my settings I have the same url and $app_url
Website with facebook -> site url -> https://www.facebook.com/pages/myappname/373671679372127/
EDIT: I edit my app_url, so it is now the same as my canvas url under app settings: http://mysite.com/index.php/
Now I don't get an error. I just get a blank window :(
EDIT: Importent to clear cookies once a while when testing. Else some very random errors somewhere
Check if you set the correct URL for "Website with Facebook Login" in the dev settings. This cannot be a page on facebook.com, it has to be a Link to YOUR domain.
Also, i would enclose the last 2 rows of your code in an "else" block. And the JavaScript code is wrong. This would be correct:
print('<script> top.location.href="' . $loginUrl . '";</script>');
(easier to understand with double quotes, you missed a semicolon. even if it´s not always needed, you better use it)
Try this code:
$user = $facebook->getUser();
// If the user has not installed the app, redirect them to the Auth Dialog
if (!$user) {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => $scope,
'redirect_uri' => $app_url,
));
print('<script> top.location.href=\'' . $loginUrl . '\';</script>');
}
else {
echo 'User ID: ' . $user;
}
Also, get rid of the Slash after "index.php". That´s not i folder (i hope/guess).

Facebook application access for the public

Ive created my first facebook app. Basically the canvas/iframe version.
It all seems ok ..except:
I can only access the apps page: (http://apps.facebook.com/hoo_promo/) when im logged in as the administrator user of the app. It should be viewable by everone. Ive submiited it to the appstore...does this matter? is this the issue?
My like-gate doesnt seem to work, even though ive followed several examples. My admin user, who im logged in with, has the page liked, but the code
$signed_request = $facebook->getSignedRequest();
$like_status = $signed_request["page"]["liked"];
for page-like is never set in the signed_request obj.
Any help much appreciated....ive added my current code...maybe this can also help others...
$app_id = "secretblahblahhiddenthis";
$secret = 'secretblahblahhiddenthis';
$canvas_page = "http://www.houseofoak.co.uk/facebook_app/";
$auth_url = "http://www.facebook.com/dialog/oauth?client_id=" . $app_id . "&redirect_uri=" . urlencode($canvas_page);
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $secret,
'cookie' => true,
));
$signed_request = $facebook->getSignedRequest();
$like_status = $signed_request["page"]["liked"];
$user = $facebook->getUser();
$page = 'unliked';
$loginUrl = $facebook->getLoginUrl(array('scope' => 'user_likes'));
$logoutUrl = $facebook->getLogoutUrl();
if (($user) && ($like_status)){
$page = 'liked';
}
For your problems:
Check to make sure you aren't in sandbox mode. Visit https://developers.facebook.com/apps/YOUR_APP_ID/advanced to check.
What you're trying to do is no longer allowed. Facebook has deprecated most of the methods that make a like gate possible. See Facebook Platform Policies Section IV. Point 1 says "You must not ... gate content behind ... Facebook social channels...."

facebook app oauth error (can not be displayed in frame)

I just wanted to create a FB app. I went to developers.facebook.com/apps, set up a new app, created a new folder on my server, copied an old (working) app into the folder, changed the app id, secret, canvas page and canvas url in my configuration file... and it did not work.
It displayed an error: "this content can not be displayed in frame".
I would be grateful if you could help me. Thanks.
I copy here my config file: (I intentionally removed the app id and secret here.)
// Facebook App ID/API Key
$appId = '';
// Facebook App Secret
$secret = '';
// Facebook Canvas Page
$redirect_uri = 'https://apps.facebook.com/appname/';
// Facebook Canvas URL
$canvas_url = 'https://myhost.com/appname/';
// Facebook App Permissions
$scope = 'email, publish_actions';
// Configure Facebook connection
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $secret,
'cookie' => true,
));
$user = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl(array(
'redirect_uri' => $redirect_uri,
'scope' => $scope,
));
$logoutUrl = $facebook->getLogoutUrl();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
$user = null;
}
} else {
header("Location: $loginUrl");
}
You should use JavaScript to redirect the user to the login URL using top.location = '{url}', and then use the login redirect url to send them back to your application. Facebook doesn't allow you to use facebook within an iframe, that why you are seeing that error message - pretty self explanatory.
It displays a "this content can not be displayed in frame" errormessage.
And what about this message gives you trouble understanding?
You can't call the auth dialog in an iframe, because it would enable phishing if the user can’t check the sites address.

Facebook Cant get /me/likes data for part of users

This is the simplest application that can be made:
<?php
include_once "inc/fb/facebook.php";
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'yyy'
));
$user = $facebook->getUser();
var_dump( $facebook->api("/me/likes/") );
?>`
In return I should get an array of pages, that user have already liked. The problem is, that it doesnt work for some users.
Somehow in a process, I have found that error:
Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user. thrown in (...)/fb/base_facebook.php on line 1039
I have tryed to force the get token with:
$access_token = $facebook->getAccessToken();
and it gests the token for every user, but still no table about what user likes.
Any help would be appreciated.
The problem was quite simple:
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'publish_stream,user_about_me',
'redirect_uri' => $mySettings->app_url,
));
echo "<script> top.location.href='" . $loginUrl . "'</script>";
I have forgoten about user_likes permission in scope. But what is strange to me - why did it worked for most of the users?