I'm learning about Facebook apps programming and I've wroted the next code that works OK, but if I comment the indicated line in the "do-login.php" file, then when I call getUser() on "index.php" will return 0.
I'm curious about that and I think maybe I'am doing something bad.
At this point, I think it wouldn't be necessary to create a Facebook object in the "do-login.php" file.
Finally, note that the link on "index.php" file points to the Heroku URL, and the link on "do-login.php" file points to the "apps.facebook.com" URL... this is the only way I found to the code works.
Any advice, please ? I'm confussed with this and I spent a day and half to get working this
Thanks!
index.php
<?php
require_once("facebook.php");
$facebook = new Facebook(array(
'appId' => THE_APP_ID,
'secret' => THE_APP_SECRET,
'fileUpload' => false,
'cookie' => true,
'trustForwarded' => true,
'allowSignedRequest' => false
));
$user = $facebook->getUser();
if( $user )
{
try
{
$user_info = $facebook->api(THE_USER_ID?fields=id,name,first_name,last_name,email&access_token='.$facebook->getAccessToken());
echo "getUser(): ".$user."<br/>";
echo "<b>Name</b>: ".$user_info['name'];
}
catch( FacebookApiException $e )
{
echo $e->getMessage();
}
}
else
{
$facebook_login_url = $facebook->getLoginUrl(array(
'scope' => 'email, read_stream, user_interests, user_likes, user_location, user_status',
'redirect_uri' => 'https://MY-APP.herokuapp.com/do-login.php'
));
echo "<p><a href='".$facebook_login_url."' target='_top'>Login</a></p>";
}
?>
do-login.php
<?php
require_once("facebook.php");
if( isset($_GET['code']) )
{
echo "<p><b>¡¡ Se autorizó la app !!</b></p>";
$facebook = new Facebook(array(
'appId' => THE_APP_ID,
'secret' => THE_APP_SECRET,
'fileUpload' => false,
'cookie' => true,
'trustForwarded' => true,
'allowSignedRequest' => false
));
// PROBLEM IS that if I comment this line, getUser() will return 0 when called in index.php
echo $facebook->getUser();
echo "<p><a href='https://apps.facebook.com/MY-APP/index.php' >Go back</a></p>";
}
else
{
echo "Error: ".$_GET['error']."<br />";
echo "Description: ".$_GET['error_description'];
}
?>
Related
Have been trying for weeks, but my App won't ask for extended permissions, obviously that means that it wont POST to the timeline afterwards either.
require_once('/wp-content/php-sdk/src/facebook.php');
$facebook = new Facebook(array(
'appId' => 'xxxxxx',
'secret' => 'xxxxxxx',
));
$user = $facebook->getUser();
if ($userID) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$ret_obj = $facebook->api('/me/feed', 'POST',
array(
'link' => 'www.theaandrdepartment.com/raw',
'message' => 'I just helped an Aussie band make it to
radio play! By voting, you can too.'
));
echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
} else { $login_url = $facebook->getLoginUrl(array(
'scope' => 'publish_stream'
));
header("Location: ".$login_url);
}
If anyone could help that would amazing.
I copied most of this code straight from the Facebook example, but to no avail.
<?php
$fb = new Facebook(array(
'appId' => $fb_config['app_id'],
'secret' => $fb_config['secret'],
));
$params = array(
'scope' => 'email, read_stream, user_interests, user_likes, user_location, user_status',
'redirect_uri' => 'http://myurl/facebook_connect',
);
$fb_login_url = $fb->getLoginUrl($params);
$this->redirect($fb_login_url);
$user = $fb->getUser();
print_r($user); // returns 0
?>
In the example above, if I redirect to the getLoginUrl result, the getUser() method returns zero.
Is there a reason why this is happening?
Replace Facebook PHP SDK path, appId, secret and redirect_uri with your app settings and Try:
require_once dirname(__FILE__) . '/facebook/facebook.php'; // You need Facebook PHP SDK
$facebook = new Facebook(array(
'appId' => $fb_config['app_id'], // Your Facebook app id
'secret' => $fb_config['secret'], // Your Facebook app secret
'cookie' => true
));
$fb_user_id = $facebook->getUser();
if ($fb_user_id) {
try {
$fb_user_profile = $facebook->api($fb_user_id);
var_dump($fb_user_profile);
} catch (FacebookApiException $e) {
error_log($e);
}
} else {
$params = array(
'scope' => 'email, read_stream, user_interests, user_likes, user_location, user_status',
'redirect_uri' => 'http://myurl/facebook_connect', // Replace with your app url
);
$facebook_login_url = $facebook->getLoginUrl($params);
echo '<script>top.location="' . $facebook_login_url . '";</script>';
exit();
}
I've used the following code. It works fine without 'scheduled_publish_time', otherwise I get this error "(#100) You cannot specify a scheduled publish time on a published post".
I've previously registered my app with another piece of code. It's so weird.
include_once("inc/facebook.php"); //include facebook SDK
$appId = '21xxxxxxxxxxx'; //Facebook App ID
$appSecret = '6b8f4bxxxxxxxxxxxxxd56'; // Facebook App Secret
$return_url = 'http://localhost:8888/...'; //return url (url to script)
$homeurl = 'http://localhost:8888/...'; //return to home
$fbPermissions = 'publish_stream,manage_pages'; //Required facebook permissions
//Call Facebook API
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $appSecret,
'cookie' => true,
'fileUpload' => true
));
$accounts = $facebook->api('/me/accounts');
$PAGE_ID = get_option('fb_post_cron_page'); // it is an option saved in WordPress
foreach($accounts['data'] as $account){
if($account['id'] == $PAGE_ID){
$ACCESS_TOKEN = $account['access_token'];
}
}
$post_url = '/'.$PAGE_ID.'/photos';
$upload_dir = wp_upload_dir();
$upload_dir= $upload_dir['path'];
$timezone= 'Europe/Rome';
$date = new DateTime($dateStr, new DateTimeZone($timezone));
//posts message on page statues
$args = array(
'access_token' => $ACCESS_TOKEN,
'source' => '#' . $image_abs_path,
'message' => $post_message,
'published' => true,
'scheduled_publish_time' => $date->getTimestamp()
);
try {
$postResult = $facebook->api($post_url, 'post', $args );
} catch (FacebookApiException $e) {
echo $e->getMessage();
}
you have to set 'published' to false
$args = array(
'access_token' => $ACCESS_TOKEN,
'source' => '#' . $image_abs_path,
'message' => $post_message,
'published' => false,
'scheduled_publish_time' => $date->getTimestamp()
);
I'm having problem when using
$facebook->api('/me','GET');
When loading the page for the first time, nothing happen. I have to reload the page again to make the script work. Not sure where the problem is.
require_once('src/facebook.php');
$app_id = "APP_ID";
$app_secret = "APP_SECRET";
// Init facebook api.
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true,
));
//Facebook Authentication part
$user_id = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'publish_stream, user_likes'
)
);
if (!$user_id) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
$user_profile = $facebook->api('/me','GET');
$user_gender = $user_profile['gender'];
if($user_gender == 'male'){
echo "you are male";
} else {
if ($user_gender == 'female'){
echo "you are female";
} else{
echo "gender not specified";
}
}
I tried take the $facebook->api('/me','GET'); out and echo $user_id, for example, it works.
As per usual, FB has me pulling out my hair.
I've been able to test my app in IE9, but when using Firefox, after a user authorizes the canvas app it goes into a redirect loop, adding state and code variables to the URL.
I'm using the javascript and php sdk with this code:
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $application_secret,
'cookie' => true, // enable optional cookie support
));
$user = $facebook->getUser();
if(!($user))
{
echo"<script> top.location.href='" . $facebook->getLoginUrl(array('redirect_uri'
=> $fbconfig['appBaseUrl'],
'scope' => 'manage_notifications,publish_stream,publish_actions'
)) . "'</script>";
exit();
}
I read about adding this:
if (window.location.hash =='#=') window.location.hash=''; but it didn't seem to do anything.
I had the same issue on my latest app.
Solved it by using the code above in the < head > section.
Don't forget to upgrade your PHP SDK to the latest version.
<?
$facebook = new Facebook(array(
'appId' => 'XXX',
'secret' => 'YYY',
));
$user = $facebook->getUser();
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'user_likes'
));
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
?>