I created an App (inside a Page Tab).
Now I check if the App has all permissions, if not I ask for them:
//$app_url = Url to Page Tab
'<script>top.location.href = "'.$facebook->getLoginUrl('next' => $app_url, 'scope' => $scope).'";</script>'
Now I want that it redirect to the Page Tab (App) after asking for permissions, but it always redirects to my domain.
Tried it first with 'redirect_uri', but that throws errors (outdated), and also with 'next'.
Any ideas, except checking in the APP if the User is on Facebook?
ps. couldn't find a up to date solution.
edit:
Just saw when I try the app as a admin the following error is displayed:
API Error Code: 191
API Error Description: The specified URL is not owned by the application
For redirect to your app Tab, if you use PHP Facebook SDK you can do:
$signedRequest = $facebook->getSignedRequest();
if(!empty($signedRequest) && !empty($signedRequest['page']['id'])){
$page = $facebook->api($signedRequest['page']);
}
$redirect_uri = $page['link'] . '?sk=app_' . $facebook->getAppId();
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'publish_actions',
'redirect_uri' => $redirect_uri
));
And then, in your HTML you put:
<script>top.location.href = "<?php echo $loginUrl; ?>";</script>
Also, in your APP configuration you have to point (Page Tab URL & Secure Page Tab URL) to your domain and folder where is your application.
Related
I've been looking everywhere for a solution to my problem, but to no avail so far.
I'm following steps to create a Facebook app from a Facebook Tutorial and I've run into a problem in the logging in process even though I seem to be doing everything correctly. When the app tries to login, I get the following error:
Given URL is not permitted by the application configuration.: One or
more of the given URLs is not allowed by the App's settings. It must
match the Website URL or Canvas URL, or the domain must be a subdomain
of one of the App's domains.
You would think that there's something wrong with my app configuration, but that doesn't seem to be the case. Here's a screenshot:
The canvas URL is correct and I've tested with or without the App Domains field filled out. Here is the php for the login:
<?php
require 'server/fb-php-sdk/facebook.php';
$app_id = '236534686497558';
$app_secret = '***************************';
$app_namespace = 'mytestsmash';
$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>');
}
?>
I can't figure out what's going wrong here.
Realized the issue was the $app_url was httpS in the php... but the canvas page url in the settings was just http!
I want the user to be redirected to a particular webpage after logging in using Facebook. I created a LoginUrl using Facebook PHP SDK and the user clicks on this link to log in. I followed http://developers.facebook.com/docs/reference/php/facebook-getLoginUrl/ when using the $facebook->getLoginUrl()
Problem: If I were to include the redirect_uri parameter, Facebook gives me an error An error occurred. Please try again later. The login url works fine without including the redirect_uri.
Anyone knows how to fix this? Thanks!
Non-working PHP Code
$loginUrl = $facebook->getLoginUrl(array(
"scope" => "email,user_education_history,user_work_history",
"redirect_uri" => "http://mydomain.com/login/facebook"
));
Working PHP Code
$loginUrl = $facebook->getLoginUrl(array(
"scope" => "email,user_education_history,user_work_history",
));
I had this same exact problem. FB changed the parameter from redirect_url to next from v2 to v3, and is poooooorly documented. Try next, should work for you.
It's pretty old topic, but here is the solution I've found working, if someone reads it later... First thing is that you have missed the comma after "redirect_uri" in your "Non-working PHP Code". And if you have "App on Facebook" selected in your application settings (app in Canvas) then your redirect_uri must be the same as the url you have set for Canvas App. If you are using just Page Tab, the redirect_uri can be anything.
$loginUrl = $facebook->getLoginUrl(array(
"scope" => "email,user_education_history,user_work_history",
"redirect_uri" => "http://mydomain.com/login/facebook",
));
My code cannot display the permission dialog when a user connect to my Facebook application. Instead of the login page, I got a 404 error.
My code:
require 'src/facebook.php';
require './config.php';
$facebook = new Facebook(array(
'appId' => $fb_app_id,
'secret' => $fb_secret,
'cookie' => true,
));
$userData = null;
$user = $facebook->getUser();
if ($user) {
try {
$userData = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'publish_stream',
));
}
<div id="loginWrapper">
<?php if ($user): ?>
Logout
<?php else: ?>
Login with Facebook
<?php endif ?>
</div>
I have also tried to put directly an personalized URL (like bellow) and I got the same 404 error.
$loginUrl = "http://www.facebook.com/dialog/oauth?client_id=".$fb_app_id."&redirect_uri=".$fb_app_url."&scope=publish_stream";
UPDATE 1:
Otherwise when I try to Login directly through the server (from http://mydomain/application/index.php), I got a redirection to Facebook website to the URL below and got this error message:
https://www.facebook.com/dialog/oauth?client_id=xxxxxxxxxxxxxx&redirect_uri=http://apps.facebook.com/myapplication&state=f8cd945d8a9aefcd0b439fecd8515a68&canvas=1&fbconnect=0&scope=publish_stream
UPDATE 2:
Here my application configuration:
And the message I got when I try to access directly from the app link: apps.facebook.com/myapplication/ without be logged
I would appreciate any help.
The status_update permission you are using in the scope for the loginUrl is not a valid permission. Take a look at the docs.
That could be your problem.
Also remember, that if a user has already authorized your app, they will not see the permission dialog again on subsequent logins. They will only be taken through the facebook login procedure.
EDIT:
Take a look at the image I've attached, facebook will on redirect to the url you specify in the site url field in app settings:
Regarding the update of your post: Go to the Developer App and select the App you're getting this error for. Click edit settings and enter the domain your App is hosted on/you're redirecting to into the "App Domain" field. Now just save and it should work.
Edit (solution from comment above): If it just doesn't work when you're not logged in, may it be that your App runs in Sandbox Mode? You can check if it's enabled in the "Advanced Settings" of your App.
I'm upgrading my existing FB apps, and going absolutely bonkers trying to get a simple PHP iframe canvas app to authorize and authenticate (as well as use SSL). Never looked through so many examples...
Here's where I'm stuck: After the user authorizes the app, and the app authenticates the user (I am able to make a graph request with the token OK), the redirect_uri happens, and the whole page refreshes, leaving Facebook and thenjust shows me the contents of my "Canvas URL" page (with my server's domain), rather than iframed on Facebook.
I currently have this as a crude two step process...
Here's what my code looks like on the first page (index.php):
<?php
require('src/facebook.php');
$app_id = '123456789';
$app_secret = '1234secrets1234';
$canvas_page = "https://apps.facebook.com/123456789/";
$canvas_url = "https://myserver.com/apptest/";
$code = $_REQUEST['code'];
if(!$code){
$display= 'page';
$scope= 'manage_pages, offline_access, read_insights, publish_stream, user_about_me, user_likes, email';
$redirect_url = 'https://myserver.com/apptest/step2.php';
$oauth_url = 'https://www.facebook.com/dialog/oauth?canvas=1&client_id='.$app_id.'&display='.$display.'&redirect_uri='.urlencode($redirect_url).'&scope='.$scope;
$config = array('appId' => $app_id,'secret' => $app_secret,'cookie' => true,'domain' => true);
$facebook_client = new Facebook($config);
echo "<script type=\"text/javascript\">top.location.href = \"".$oauth_url."\";</script>";
}
?>
and the second page (step2.php):
<?php
require('src/facebook.php');
$app_id = '123456789';
$app_secret = '1234secrets1234';
$canvas_page = "https://apps.facebook.com/123456789/";
$canvas_url = "https://myserver.com/apptest/";
if($_REQUEST['code']){
$code=$_REQUEST['code'];
$redirect_url = 'https://myserver.com/apptest/step2.php';
$link="https://graph.facebook.com/oauth/access_token?canvas=1&client_id=".$app_id."&redirect_uri=".urlencode($redirect_url)."&client_secret=".$app_secret."&code=".$code;
$string = file_get_contents($link);
$auth_token=substr($string, 13, 150);
$graph_url = "https://graph.facebook.com/me?access_token=".$auth_token;
$user = json_decode(file_get_contents($graph_url));
echo("Hello " . $user->name);
}
Again, once the user has authorized the app, and the app has authenticated the user, the graph call works.
Any ideas?
When navigating to the OAuth dialog the web page (not the frame your app is in) is navigated to the OAuth URL. To get back into the Facebook iframe after authentication you need to set the OAuth redirect URL to the canvas_page URL. The code shown above is navigating to the URL of myserver when redirected so your app takes up the entire page (because you left the Facebook iframe when navigating to the OAuth dialog). Your code at canvas_url needs to determine if it is being entered from authorization (success or failure) or if it is being entered with a valid access token after authentication.
Also your canvas_page URL appears to be comprised of the facebook apps host and your application ID. It should be the facebook apps host and your application name (the redirect URL should be the same as the "Canvas Page" URL on your app's developer page).
Well I did get this working. On the app > settings > basic I hadn't set a namespace, so the URL it gave me for the app on facebook was like this: https://apps.facebook.com/123456789/ and now with the namespace they changed it to: https://apps.facebook.com/myappname. So that may have been it. I tried to carefully follow the simple PHP autorization demo on this page: https://developers.facebook.com/docs/appsonfacebook/tutorial/ and it seems to work ok now.
Thanks for the help!
I have little problem. When I need my app to ask user to grant permissions to the app I use following code:
<?php
$loginUrl = $facebook->getLoginUrl(
array(
'canvas' => 1,
'fbconnect' => 0,
'scope' => 'email,publish_stream,offline_access',
));
?>
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
I works OK, but after user press "grant permissions" he is redirected to my app URL. I mean like http://mydomain.com/myapp/ directly, not my app page on Facebook.
I can work around it - I know my app's Facebook address, so I redirect him to proper http://apps.facebook.com/myappname. It works OK too. BUT - when somebody adds my application on some page tab - here is where problem appears. Because I don't know where to redirect user anymore - I don't know, from what page my app was called.
Is there any way to get to know, from what page my app was called, or, even better, to make Facebook properly redirect user to the "facebook page" where app tab were added after "grant permission" dialog ?
You can set redirect_uri like so:
<?php
$loginUrl = $facebook->getLoginUrl(
array(
'canvas' => 1,
'fbconnect' => 0,
'scope' => 'email,publish_stream,offline_access',
'redirect_uri' => 'http://yourdomain.com/app', //the url to go to after a successful login
));
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
?>
Seems to be the only thing you can do is detect if the user is viewing your page from outside of facebook and then redirect them to your facebook app page.