Use Facebook Api Locally - php

I am attempting to use the facebook api with my php web application. I have downlaoded the api and in my app settings is added a website platform with the Site URL as my localhost address http://localhost:8080/app/ for testing.
However it does't add to the seetings with this, when I try to save this in the App Dashboard I get an error message, but when I set it as http://localhost/app/, it saves it.
I am doing this to sort out the error I get when I try to use the api.
When I go to http://localhost:8080/app/, the page redirects to:
https://www.facebook.com/dialog/oauth?client_id=648801771829346&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Fkfc%2Ffacebook_connect&state=1e655991943d79a58aac8d22fbd2c61f&sdk=php-sdk-3.2.3
with the 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.
My code:
<?php
require_once('facebook-php-sdk/src/facebook.php');
// Create our Application instance (replace this with your appId and secret).
$facebook= new Facebook(array(
'appId' => '648801771829346',
'secret' => 'd4a528ad0614314a9d3aaf59c978fbfa',
));
$user = $facebook->getUser();
if ($user) {
try {
// Get the user profile data you have permission to view
$user_profile = $facebook->api('/me');
echo "<pre>";
print_r($user_profile);
echo "</pre>";
} catch (FacebookApiException $e) {
$user = null;
}
} else {
die('<script>top.location.href="'.$facebook->getLoginUrl().'";</script>');
}
How do I fix this do that I can test locally on http://localhost:8080/app/, this is my xampp server.
DashBoard:

First, Add a platform -> Website
Settings -> Basics -> App on Facebook -> Canvas URL -> http://localhost/app?
Settings -> Basics -> Website -> Site URL as http://localhost/app/

There is a toggle button in the Settings->Advanced part of your application which says "Native or desktop app? Enable if your app is a native or desktop app". When you turn on that switch, I think you will be able to send back to localhost the information from Facebook.

Related

Facebook Graph search API

Background:
I'm an employee of a public figure who is constantly getting facebook scam impersonations. I'm going to automate the process of checking facebook for new scams by having a script search facebook each day and report any new users and pages for his name. I'm modifying an old FB app that I created years ago to run the process.
Problem:
I noticed that I can search for "page" on facebook's graph api with my app, but "user" comes back with: "Fatal error: Uncaught OAuthException: A user access token is required to request this resource." I assume this is a permissions error so I try to add an &Access_Token= with all permissions from (https://developers.facebook.com/tools/explorer) to the URL with no luck.
Here's my PHP script:
require '../src/facebook.php';<BR>
$facebook = new Facebook(array(
'appId' => '**I_removed_this_code**',
'secret' => '**I_removed_this_code**'));
$access_token = $facebook->getAccessToken();
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');<BR>
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
$fb_response = $facebook->api('/search?q=**Public_Figure_Name**&type=page'); //<--change this to user and it doesnt work
print_r(array_values($fb_response));
The basic issue is that only users an app has access to are either users of the app or (given proper friend permissions) friends of users of the app.
However, if you were to act as you, using an SSO token, 'you' would have a user token and can do graph searches.
PS. I would have added this as a comment but my rep's not high enough.
If I try the following in the Graph Explorer, it works without problem:
/search?q=Tom%20Cruise&type=user
https://developers.facebook.com/tools/explorer?method=GET&path=search%3Fq%3DTom%2520Cruise%26type%3Duser&version=v2.2
The Search API is documented at https://developers.facebook.com/docs/graph-api/using-graph-api/v2.2#search which describes exactly your behaviour (distinction between page and user search).

Facebook getUser() not working

I am trying to connect to the php facebook sdk and pull user data.
<?php
require_once('facebook-php-sdk/src/facebook.php');
// Create our Application instance (replace this with your appId and secret).
$facebook= new Facebook(array(
'appId' => '2453463636',
'secret' => '365653656565656',
'allowSignedRequest' => false,
));
$user = $facebook->getUser();
if ($user) {
try {
// Get the user profile data you have permission to view
// $user_profile = $facebook->api('/me');
$user_profile = $facebook->api('/me?fields=picture.width(100).height(100),first_name,last_name,username,email');
echo $user_profile['email'];
} catch (FacebookApiException $e) {
$user = null;
}
} else {
die('<script>top.location.href="'.$facebook->getLoginUrl().'";</script>');
}
?>
This works well on localhost while testing. I have a app created on facebook for this with a website platform added with the locahost as the site url in the dashboard.
To now test on the server I created another app on facebook with the domain and site url set as my staging site on my server.
I simply changed the appId and secret to that of the Facebook App created with the remote staging site url address, but this doesn't work.
Help Please! Is there something key that I missed in the code or dashboard that is not causing my page to get the user?
My page is a view in a codeigniter application, and require_once('facebook-php-sdk/src/facebook.php');, this directory resides in the root of my codeigniter application
EDIT:
I replace the die clause with die("failed");, and the text "failed" shows up when I try to load the page. Not sure what the problem is? This worked fine on localhost, why am I noy connecting now?
When I print_r($user); the page returns 0. Why am I not connecting.

What is Facebook API error 191 [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Facebook API error 191
I am getting the following error with some code that I am using. The error is
API Error Code: 191
API Error Description: The specified URL is not owned by the application
Error Message: redirect_uri is not owned by the application.
<?php
$facebook = new Facebook(array('appId' => $app_id,'secret' => $app_secret,'cookie' => true));
if($facebook->getUser() < 1)
{
$red_url = $page_url.'?sk=app_'.$app_id;
$redir = $facebook->getLoginUrl(array('redirect_uri'=>$red_url,'next'=>$red_url,'scope'=>'offline_access,publish_stream,status_update,photo_upload,user_birthday'));
echo "<script>top.location.href='".$redir."';</script>";
exit;
}
$user = $facebook->api('/me');
Can any one explain how to get around this and why this happens?
When you open a Facebook application you need to set the domain/s under which your application is intended to run - and (almost) every place that your app gets in contact with facebook (especially client side) must be from a url from the same domain (or a subdomain of it)
in your case you told asked facebook to authorize the app for the user and then redirect him to $red_url which I understand to be the page where your app is installed - BUT this link is not under the domain of your application (unless you registered facebook.com as your app domain in the application dashboard
if you want to redirect the user to that specific tab - you may create a proxy file under the domain of your application that will redirect the user to the tab, for example:
lets say you registered mydomain.com as your app domain in the app dashboard . then - create a file named redirect.php for example that will conatin the following script and put it under http://www.mydomain.com/my_directory/redirect.php :
<?php
$app_id ="ENTER_YOUR_APP_ID_HERE";
$page_url = "ENTER_THE_PAGE_URL_HERE"; //for example: http://www.facebook.com/techmarketing.co.il
$red_url = $page_url.'?sk=app_'.$app_id;
header("Location: {$red_url}");
and your script will change to be:
<?php
$facebook = new Facebook(array('appId' => $app_id,'secret' => $app_secret,'cookie' => true));
if($facebook->getUser()==0)
{
$red_url = "http://www.mydomain.com/my_directory/redirect.php";
$redir = $facebook->getLoginUrl(array('redirect_uri'=>$red_url,'next'=>$red_url,'scope'=>'offline_access,publish_stream,status_update,photo_upload,user_birthday'));
echo "<script>top.location.href='".$redir."';</script>";
exit;
}
$user = $facebook->api('/me');
Can you say "cross site scripting" ;)?
WORKAROUND:
Browser, Edit Setting, Web Site
<= add site URL to the app settings
Here's a bit more background:
*
http://techblog.hybris.com/2012/06/05/oauth2-the-implicit-flow-aka-as-the-client-side-flow/
redirect_uri: The server configured a redirect_uri (which we strongly recommend)
which needs to match the settings for the client_id. Client_id and
redirect_uri are both server-side settings that the app developer
needs to get at beforehand.
You need to tell Facebook that your app is allowed access to that website.
Edit your app settings (via the FB developer dashboard). On the basic settings page, click on 'Website with Facebook Login' and enter your site address.

Facebook PHP SDK: how to display login dialog automatically?

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.

Facebook Application with PHP running losing session

Iam trying to build an Facebook Application based on PHP. The Application is running under php on my own Webhost inside an Canvas as iFrame.
I have included the newest Client Library for PHP from Facebook: facebook-php-sdk-94fcb13
To Authorize the user inside my application iam trying to use Facebook Connect, like the example shipped with the Client. Everything works fine the 1st Login, but when i hit the F5 Key to reload the page, the session is lost and i have to login again. When i call my application outside of the Facebook Canvas everything is fine.
Iam not sure, but i think my Browser (Chrome/FireFox - Ubuntu) is not allowing to store an cookie inside an iFrame.
Does someone knows an solution for this Problem? Here are some Parts of the Sourcecode:
$facebook = new Facebook(array(
'appId' => 'x',
'secret' => 'x',
'cookie' => 'true',
));
$session = $facebook->getSession();
$facebook->setSession($session);
$me = null;
// Session based API call.
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
}
}
// login or logout url will be needed depending on current user state.
if ($me) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
Solved :-)
I have missed to setup the basedomain in configuration.
On the "Conntect" Tab you have to set the Basedomain to your host.
Example: Application is running under http://myapp.com/facebook/html than set the basedomain
to myapp.com
Now the cookie is stored correctly.
php passes the session id via a cookie value , if its not able to send it , sends via get parameter like *.php?phpsessid......
It will get lost if u use header() redirection , solution append .SID in url,
like
header("blah blah".SID);
Hope it helps you..

Categories