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...."
Related
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).
i am creating a fanpage and attach in my facebook apps.
i want to invite my friends form my facebook page to like page and get some offer
so i am using code to display status.
<?php
require 'facebook.php';
$app_id = "xxxxxxxxxxxx";
$app_secret = "xxxxxxxxxxxxxxxxxxxxxx";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$user = $facebook->getUser();
$signed_request = $facebook->getSignedRequest();
$page_id = $signed_request["page"]["id"];
$page_admin = $signed_request["page"]["admin"];
$like_status = $signed_request["page"]["liked"];
$country = $signed_request["user"]["country"];
$locale = $signed_request["user"]["locale"];
?>
here using facebook api to get pageid or user id and other information .
There is no API which allows you to invite a user to like a page, this is only available in Facebook's frontend
I have the same issue but I intend to use (Requests)
https://developers.facebook.com/docs/reference/dialogs/requests/
You can send their friends a request asking them to like your page ! I guess this is more effective and you decide what should be written in the request!
i am trying to post to my friends' feeds using this code, but it is not working . i am stuck, any help ??
$app_url ="http://localhost.local/PMS/facebook/PostWithPHP.php";
$facebook = new Facebook(array(
'appId' => 'APPID',
'secret' => 'APPSECRET',
'cookie' => true,
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
$user_friends = $facebook->api('/me/friends');
sort($user_friends['data']);
try {
// Proceed knowing you have a logged in user who's authenticated.
$access_token = $facebook->getAccessToken();
$vars = array(
'message' => 'My Message',
'name' => 'title',
'caption' => 'Caption',
'link' => 'Link',
'description' => 'Description',
'picture' => 'image'
);
foreach($user_friends['data'] as $f){
$sendTo = $f['id'];
$sendToName = $f['name'];
$result = $facebook->api("/".$sendTo ."/feed", 'post', $vars);
}
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array('redirect_uri'=> $app_url));
echo "<script type='text/javascript'>";
echo "top.location.href = '{$loginUrl}';";
echo "</script>";
}
and another question is that using this code, but with replacing $facebook->api("/".$sendTo ."/feed", 'post', $vars); by $facebook->api("/me/feed", 'post', $vars); and of course without looping friends, posts on my timeline. how can i make it post on my wall ??
I guess for a post to a timeline you will need an accessToken from the user where to publish the content. In your case you just have the accessToken of the registered user, not of his friends. That is a restriction by FB I think.
1st check you getting the user id(place echo and check) if not try this... I think this will help you brother
$token_url = "https://graph.facebook.com/oauth/access_token?" ."client_id=" . $app_id ."&client_secret=" . $app_secret .
"&grant_type=client_credentials";
$access_token = file_get_contents($token_url);
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
$user_id = $data["user_id"];
There are a few things wrong with your code. First of all, make sure the link and picture parameters for the post are valid URLs. Facebook will give you a error message otherwise ((#100) link URL is not properly formatted). Also, the link must go to the Canvas or Site URL for your application.
That should solve the issues with posting to a friend's wall.
However, may I remind you that your application is in violation of the Facebook Platform Policy. Facebook doesn't allow multiple posts to the stream (whether its yours or a friends) unless there is explicit permission from the user. It also to stop common friends seeing the same message from multiple friends.
You can follow the tutorial here:
https://www.webniraj.com/2012/11/22/facebook-api-posting-a-status-update/
But, instead of making a API call to: /me/feed, you replace me with the friend's User ID, so it looks like /12345/feed
Please note that Facebook has now disabled posting to Friends' walls via the API. Instead, you should either tag the user in an action or use the Requests API.
I hope that someone can help me. I am developing a Iframe application. I am using a mixture of PHP and Javascipt.
Everything works perfectly if you click on the facebook tab created in the facebook profile. However I have a second page which edit the some functionality in my page, If I navigate to this page, it seems to loose it's authentication, or login session.
The like button, goes to not-liked - (so it seems to me the session breaks).
How can I keep the facebook session, to the next page?
I have included the complete page of my nav.php (This is the page that facebook first access)
<?php require 'facebook.php';
$app_id = "MY-APP-ID";
$app_secret = "MY-APP-SECRET";
$apiKey = "1573bcaff76885d24c5ca93d93266a9e";
$app_url = "http://domain.com";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
if ($_SERVER['HTTP_REFERER'] != 'http://domain.com/facebook/edit_page.php');
{
$signed_request = $facebook->getSignedRequest();
$page_id = $signed_request["page"]["id"];
$page_admin = $signed_request["page"]["admin"];
$like_status = $signed_request["page"]["liked"];
$country = $signed_request["user"]["country"];
$locale = $signed_request["user"]["locale"];
if ($page_admin == 1) {
$oauth_token = $signed_request["oauth_token"];
}
}
if ($like_status) {
// If you like the page this will show - START
}
else {}
if ($page_id == '12345') {
echo "content for 12344";
}
elseif ($page_id == '56789') {
echo "content for 56789";
} ?>
In this page I can see who is the owner of the page, and the LIKE button shows liked! how do I pass the authentication to a next page? so that the Like button stay Liked?
Any help would be appreciated!
Thanks
I have figured out the problem - In my case I needed to pass the getSignedRequest(); to all pages, for the session to stay in place.
I wrote a facebook application but authentication is not working properly. Despite of googling several hours it did not solve.
please please write the script which authenticates the user properly and if the user clicks the allow button simply display the text "Welcome" assuming that i have written all basic suff aas..
require 'sdk/src/facebook.php';
$app_id = 'MY APP ID';
$canvas_page = "MY CANVAS URL";
// 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&installed=1";
$facebook = new Facebook(array(
'appId' => $app_id ,
'secret' => MY APP SECRET,
'cookie' => true,
));
///... your code to solve !
Nobody provides the ultimate solution: Finally I solved it by myself.
what I do is. Facebook sends an "installed=1" parameter with the url when the user allows the application. SO I checked this parameter and redirected again to the application. I m extremely happy on finding extremely nice and efficient solution. Nobody answered me any votable answer.
if(isset($_REQUEST['installed']))
echo("<script>top.href.location='http://apps.facebook/myapp' </script>");
It seems that you are mixing the examples from the documentations and the PHP-SDK, dropping the example from the PHP-SDK should work.
Following the code in this answer:
<?php
require '../src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXXXXXX',
'secret' => 'XXXXXXXX',
'cookie' => true,
));
$session = $facebook->getSession();
$loginUrl = $facebook->getLoginUrl();
$me = null;
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
echo "Welcome User: " . $me['name'] . "<br />";
} catch (FacebookApiException $e) {
error_log($e);
}
} else {
echo("<script> top.location.href='" . $loginUrl . "'</script>");
}
?>