I'm trying to write an app that will post images the user makes to a user's wall in facebook, it was working until about an hour ago now i'm getting a catch error of "An active access token must be used to query information about the current user.". hopefully someone has a quick answer :)
here is my starting PHP that gets the user and checks for the album
<?php
include 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => ID,
'secret' => SECRET,
'cookie' => true
));
$me = null;
$thisPhoto = null;
$album_name = NAME;
$album_message = MESSAGE;
try {
$me = $facebook->api('/me');
$facebook->setFileUploadSupport(true);
// check if album with name exists...if not create it
$albums = $facebook->api('/me/albums');
foreach($albums['data'] as $album) {
if($album['name'] == $album_name) {
$album_uid = $album['id'];
}
}
if (!$album_uid) {
$album_details = array(
'message'=> $album_message,
'name'=> $album_name
);
$create_album = $facebook->api('/me/albums', 'post', $album_details);
$album_uid = $create_album['id'] . "MADE";
}
//echo "<!-- $album_uid -->\n";
}
catch (FacebookApiException $e) {
echo "<!-- " . $e->getMessage() . " -->\n";
}
?>
This is my main HTML (the above is called before the doctype)
<!doctype>
<html>
<head>
<meta charset=utf-8>
<title>'TITLE</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css" media="screen">
html, body { width:100%; background-color: #FFF;}
body { margin:0 auto; padding:0; width:760px}
#flashContent { width:100%; height:100%; }
</style>
</head>
<body>
<?php
//print_r($me);
//echo $facebook->getUser();
//echo $facebook->getAccessToken();
// START
if($me) {
// true makes print_r give human readable instead of just a screen dump
//echo '<pre>' . print_r($VARIABLE, true) . '</pre>';
$appName = NAME.SWF;
$appWidth = "760";
$appHeight = "640";
$appID = "stache_yerself";
$appAlt = "Square Shooters app - 'Stache Yerself";
// ECHOS out HTML used for the APP
}
else {
$loginUrl = $facebook->getLoginUrl(array('scope' => 'publish_stream,read_friendlists,user_photos'));
echo 'Login';
}
?>
</body>
</html>
The else above gives a link which is there; however, does nothing it just goes to an empty page. I reset the "secret" on the app's developer page but didn't help. I have no idea why it just stopped working (did facebook change their code again?). Even the login thing from the HTML section was working before and now...nothing. I'm stumped as to why this just stopped working.
Did you hit your API limit for your dev account?
You can do 600 queries in 600 seconds.
Also, this might be helpful
https://www.facebook.com/help/210701362295828/
Related
I am using the following code to post on facebook from my page but the lonin page is not loading
you can see the page
When I click on the following link the facebook required page is not loading
The code is look like that
<?php
require 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => '198516340340394',
'secret' => 'f11117b96e0996ecbf7d7f4919c0cf70',
'cookie' => true
));
$user = $facebook->getUser();
$user_profile = null;
if ($user) {
try {
$user_profile = $facebook->api('/me');
$facebook->api('/me/feed/', 'post', array(
'message' => 'I want to display this message on my wall'
));
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user_profile) {
$logoutUrl = $facebook->getLogoutUrl();
echo 'Logout';
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'publish_stream, read_friendlists',
));
echo 'Login';
}
?>
<html>
<head>
<title>PHP SDK</title>
</head>
<body>
</body>
</html>
Please help me in this case and please give me the detail answer, and be with me
There are a couple problems with what you're doing here.
First it's always best to have valid markup ie doctype and html tags your page is missing. but more importantly... this
echo 'Login';
is wrong. the markup is just as it looks I think you're expecting a link. Change it to
echo 'Login;
and try again. the reason is php does not recognize variables in a single quoted string.
I'm trying to do login via FB. If I'm not logged in FB, everything works fine but if I'm logged in FB and I click on "login via facebook!" hyperlink the URL has changed on comething like this fb2.php?code=AQAlKrGXWS2bhiZ-ZK7UPiDRLdVMjR84jAbxS41kW3I5h5P... but I'm not logged and if I'm not login till I click second time. It's strange, isn't it? Could you someone help me? The source code is here.
<?php session_start();
define('APP_ID', 'my_api_id');
define('APP_SECRET', 'my_app_secret');
define('CANVAS_PAGE', 'http://apps.facebook.com/xxx/');
define('CANVAS_URL', 'http://localhost/xxx/');
require_once 'facebook.php';
$facebook = new Facebook( array('appId' => APP_ID, 'secret' => APP_SECRET, ));
$user = $facebook->getUser();
if(isset($user)) {
try {
xtended_permission)
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
$user = NULL;
}
}
if ($user) { $logoutUrl = $facebook->getLogoutUrl(array('next' =>
"http://metalradio.cz/metalRadioTesting/facebook-sdk/logout.php")); }
else { $loginUrl = $facebook->getLoginUrl( array(
'scope' => 'email' ) ); }
?>
<!doctype html>
<html>
<head>
<title>Facebook login</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body>
<?php
if(!is_null($user))
echo "Name: " . $user_profile['email'];
?>
<h2>Users authentification via FB</h2>
<?php if ($user):
?>
logout!
<?php else:?>
<div>
login via facebook!
</div>
<?php endif?>
</body>
</html>
I am trying to upload an image and post to my fan page wall as a page using this tutorial
however the connection doesnt seem to work.
any suggestions how to get the page parameters so that i can post images to the pages wall please
this code has been modified from the latest SDK 3.1.1
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => '***************',
'secret' => '********************************',
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
$page_id = '***************';
$page_info = $facebook->api("/$page_id?fields=access_token");
if( !empty($page_info['access_token']) ) {
$args = array(
'access_token' => $page_info['access_token'],
'message' => "I'm a Page!"
);
$post_id = $facebook->api("/$page_id/feed","post",$args);
}
} 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(array( 'next' => 'http://myDoamin.com/logout_page.php' ));
} else {
$loginUrl = $facebook->getLoginUrl(array('scope'=>'manage_pages,publish_stream'));
}
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk</title>
<style>
body { font-family: 'Lucida Grande', Verdana, Arial, sans-serif; }
h1 a { text-decoration: none; color: #3b5998; }
h1 a:hover { text-decoration: underline; }
</style>
</head>
<body>
<h1>php-sdk</h1>
<?php if ($user): ?>
Logout
<?php else: ?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
Login with Facebook
</div>
<?php endif ?>
<h3>PHP Session</h3>
<pre><?php print_r($_SESSION); ?></pre>
<?php if ($user): ?>
<h3>You</h3>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
<h3>Your User Object (/me)</h3>
<pre><?php print_r($page_info); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>
</body>
</html>
uploading handeled here:
<?php
$app_id = "***************";
$app_secret = "********************************";
$post_login_url = "http://mydomain.com/upload.php";
$code = $_REQUEST["code"];
//Obtain the access_token with publish_stream permission
if(empty($code)){
$dialog_url= "http://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode($post_login_url)
. "&scope=publish_stream";
echo("<script>top.location.href='" . $dialog_url
. "'</script>");
}
else {
$token_url="https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url )
. "&client_secret=" . $app_secret
. "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$access_token = $params['access_token'];
// Show photo upload form to user and post to the Graph URL
$graph_url= "https://graph.facebook.com/me/photos?"
. "access_token=" .$access_token;
echo '<html><body>';
echo '<form enctype="multipart/form-data" action="'
.$graph_url .' "method="POST">';
echo 'Please choose a photo: ';
echo '<input name="source" type="file"><br/><br/>';
echo 'Say something about this photo: ';
echo '<input name="message"
type="text" value=""><br/><br/>';
echo '<input type="submit" value="Upload"/><br/>';
echo '</form>';
echo '</body></html>';
}
?>
the example from the SDK is similar and when i run this it works as user. but not as page
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
refer to: https://developers.facebook.com/docs/authentication/pages/
Grant manage_pages permissions to your application.
Get page_access_token, make api post to page as page with page token.
EXAMPLE: php cURL upload: stand alone example, no sdk needed.
<?php
function GetCH(){
// id of the album to upload
$ablum_id = "182325958494028";
// photo caption
$photo_caption = "testing app ablum upload";
// the page access token if you are uploading as the page
$page_access_token = "Add Your page token here.";
// url to your photo
$photo_url = "http://sphotos.xx.fbcdn.net/hphotos-ash3/544288_10150906170859577_732484576_9506566_417314835_n.jpg";
//
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/".$ablum_id."/photos?url=".urlencode($photo_url)."&message=".urlencode($photo_caption)."&method=post&access_token=".$page_access_token."");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$sendCH = curl_exec($ch);
curl_close($ch);
return $sendCH;
};
// call our function and upload photo, large photos may be slow
echo GetCH();
// if successful a return is shown
// { "id": "387851721274783", "post_id": "171535666239724_387851741274781" }
?>
This is how I post as a Page. I'm using Facebook's PHP SDK.
I am first fetching a list of all the pages the user has access to, then I present a list of them to the user so they can choose one (code not shown here). I then store that data and use it later to post as that page.
How I get the list of pages:
try {
$result = $facebook->api("/me/accounts");
$userpages = $result['data'];
} catch (FacebookApiException $e) {
$userpages = array();
$fbuser = null;
$facebook->clearPersistentData();
}
Now I iterate through them ($settings is an array with my user's settings, including the page they chose to post as):
foreach ($userpages as $userpage) {
if ($userpage['category'] == "Application")
continue; # My app doesn't work with Application pages.
if ($settings['page']['id'] == $userpage['id']) {
# This is the page I want.
# $userpage['access_token'] has the access token.
}
}
The access_token gets added into my array of postdata for the request, and the id is used here.
return $facebook->api("/$id/feed/", "post", $postdata);
I have a little Facebook app, that posts some preloaded images. I changed the code structure, so i moved all the variables into config.php, so it would be more comfortable. But after that the app doesn't work and i really don't know why... After the permission dialog it realoads himself over and over... Can someone fing the bug?
This is the index.php:
<?php
require 'facebook.php';
require 'config.php';
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $appSecret,
'baseUrl' => $baseUrl,
'appBaseUrl' => $appBaseUrl,
'fileUpload' => 'true',
'cookie' => 'true'
));
if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) {
$baseUrl = str_replace('http','https',$baseUrl);
$fbPageURL = str_replace('http','https',$fbPageURL);
$fbPageAppURL = str_replace('http','https',$fbPageAppURL);
}else{
// not https
}
$signed_request = $facebook->getSignedRequest();
$page_id = $signed_request["page"]["id"];
$like_status = $signed_request["page"]["liked"];
if(!$like_status){
header("Location: notfan.php");
exit;
}
$user = $facebook->getUser();
$params = array(
scope => 'publish_stream,user_photos',
redirect_uri => $fbPageAppURL
);
$loginUrl = $facebook->getLoginUrl($params);
$app_data = $signed_request["app_data"];
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title><?php echo $appName; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style>
a:link {color:#f1effc;}
a:visited {color:#f1effc;}
a:hover {color:#19378d;}
a:active {color:#19378d;}
a:link {text-decoration: none}
</style>
</head>
<body>
<script type="text/javascript">
function NotInFacebookFrame() {
return top === self;
}
function ReferrerIsFacebookApp() {
if(document.referrer) {
return document.referrer.indexOf("apps.facebook.com") != -1;
}
return false;
}
if (NotInFacebookFrame() || ReferrerIsFacebookApp()) {
top.location.replace("<?= $fbPageAppURL ?>");
}
</script>
<?php if($user){
try {
$user_profile = $facebook->api('/me');
$scope = 'publish_stream,user_photos';
$scope_params = explode(',',$scope);
$permissions = $facebook->api("/me/permissions");
if( array_key_exists('publish_stream', $permissions['data'][0]) && array_key_exists('user_photos', $permissions['data'][0])) {
// the main app logic here
} else {
$user = null;
}
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
// logged in user
} else {
// not logged in or not authenticated
echo '<script type="text/javascript">top.location.href = "'.$loginUrl .'";</script>';
}
?>
</body>
</html>
and the config.php:
<?php
$appName = 'the name';
$appId = '123456789';
$appSecret = '1324v3434v34v';
$baseUrl = 'http://www.domain.com/app-name/';
$appBaseUrl = 'http://apps.facebook.com/zzzzzzz/';
$fbPageURL = 'http://www.facebook.com/xxxxxxx';
$fbPageAppURL = $fbPageURL.'?sk=app_'.$appId;
?>
Thank you very much!!
As it turned out, it was a server configuration failure.
Wrap $user = $facebook->getUser(); in a try/catch and echo the exception that gets caught. That will tell you why the user is not getting logged on.
More details - after the comments below.
You check document referrer in your Javascript - but the actual referrer in my tests was "http://static.ak.facebook.com/platform/page_proxy.php?v=4" so this would fail that condition. This will causes a loop.
To check if you're in a canvas, just get the signed_request using the PHP SDK
$facebook->signed_request();
If this is empty then you're not in a canvas (or a page tab) so redirect to the canvas. Do that in PHP and not in Javascript.
I have a Facebook PHP SDK APP in development phase and i'm stuck on something...
The logic of the APP is this:
first the user have to like the page (app is on this) // works very well :)
after that comes the permissions request
if permission is granted: the app_data variable is pass through and the user recieves a picture.
app_data variable is necessary because, if the user clicks the app after permission granted again, it gets new picture automaticly. with the app_data don't.
my problem is this link:
$params = array(
scope => 'publish_stream,user_photos',
redirect_uri => 'http://www.facebook.com/XXXXX?sk=app_YYYYYY&app_data=1'
);
Accept
If i click on it, the app shows blank page, and nothing happening... :(
On direct call, that app runs without any error.
Can someone help me out?
// sorry for my english...
Thank you!
and the whole index.php is:
this is the whole index.php:
<?php
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxxxx',
'secret' => 'yyyyy',
'baseUrl' => 'http://hosting.address/',
'appBaseUrl' => 'http://apps.facebook.com/app-name/',
'fileUpload' => 'true',
));
// Get User ID
$user = $facebook->getUser();
$params = array(
scope => 'publish_stream,user_photos',
redirect_uri => 'http://www.facebook.com/xxxxx?sk=app_yyyyy&app_data=1'
);
$signed_request = $facebook->getSignedRequest();
$app_data = $signed_request["app_data"];
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>the title</title>
<style>
a:link {color:#ffffff;}
a:visited {color:#ffffff;}
a:hover {color:#ececec;}
a:active {color:#1f1f1f;}
a:link {text-decoration: none}
</style>
</head>
<body>
<?php if ($user){
try {
$user_profile = $facebook->api('/me');
$likes = $facebook->api("/me/likes/123123123"); //page ID
if( !empty($likes['data']) ){
$scope = 'publish_stream,user_photos';
$scope_params = explode(',',$scope);
$permissions = $facebook->api("/me/permissions");
if( array_key_exists('publish_stream', $permissions['data'][0]) && array_key_exists('user_photos', $permissions['data'][0]) && isset($app_data)) {
$file = "1.jpg";
$message = 'bla bla';
$ret_obj = $facebook->api('/me/photos', 'POST', array(
'source' => '#' . $file,
'message' => $message,
)
);
echo "<img width=\"520px\" src=\"1.jpg\" />";
} else {
?>
accept</td>
deny</td>
<?
}
}else{
echo "<img width=\"520px\" src=\"no-fan.jpg\" />";
}
} catch (FacebookApiException $e) {
echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
$user = null;
}
}
if ($user) {
} else {
$loginUrl = $facebook->getLoginUrl($params);
echo '<script type="text/javascript">top.location.href = "'.$loginUrl .'";</script>';
}
?>
</body>
</html>
I've had this before when I was delving into the world of Facebook apps.
What I eventually done was for my redirect_uri I used a page on my domain, e.g. https://domain.com/facebook/redirect.php and in redirect.php I would have:
// user didn't give app permissions
if (isset($_GET['error_reason']))
{
header( 'Location: http://www.facebook.com/FANPAGE' ) ;
}
else
{
header( 'Location: http://www.facebook.com/XXXXX?sk=app_YYYYYY' ) ;
}