I'm trying to post photo on user wall and tag friends but i'm getting this :
Fatal error: Uncaught OAuthException: (#324) Requires upload file
thrown in /home/vhosts/somesite.com/src/base_facebook.php on line 1238
This is my code
<?php
require_once "./src/facebook.php";
$app_id = "xxxxxxxx";
$app_secret = "xxxxxxxxxx";
// Init facebook api.
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
// Get the url to redirect for login to facebook
// and request permission to write on the user's wall.
$login_url = $facebook->getLoginUrl(
array('scope' => 'publish_stream')
);
$loginUrl = $facebook->getLoginUrl($params);
// If not authenticated, redirect to the facebook login dialog.
// The $login_url will take care of redirecting back to us
// after successful login.
if (! $facebook->getUser()) {
echo <<< EOT
<script type="text/javascript">
top.location.href = "$login_url";
</script>;
EOT;
exit;
}
// Do the wall post.
$args = array(
'message' => 'Test Message',
'image' => '#' . realpath('http://imageurl.com'),
'tags' => array(
array(
'tag_uid'=> $friend1_uid,
'x' => $x1,
'y' => $y1,
),
array(
'tag_uid' => $friend2_uid,
'x' => $x2,
'y' => $y2,
),/*...*/
),
);
$data = $facebook->api('/me/photos', 'post', $args);
// Upload Support.
$facebook = new Facebook(array(
/*..*/
'fileUpload' => true,
));
?>
'image' => '#' . realpath('http://imageurl.com'),
realpath is for file system paths, not for URLs.
Either change that to a local file system path – or, if you want to upload a photo from a publicly available HTTP URL, use parameter name url instead of source and give the URL as value.
Related
<?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 have problem with Facebook PHP SDK. It always throws that exception. I tried many solutions listed here, but nothing works for me.
It seems that Facebook returns to me valid access token, because I tested it with Debug tool in dashboard of my application.
What's my scenario?
I want to post to publish simple content to user's wall by calling static function:
function social_publish($network, $title, $message, $link = '', $image = '') {
global $_config;
// Initialize Facebook SDK
$facebook = new Facebook(array(
'appId' => $_config['fb_app']['app_id'],
'secret' => $_config['fb_app']['app_security_key']
));
// Set data
$attachment = array(
'name' => $title,
'caption' => $title,
'message' => $message,
'link' => $link,
'picture' => $image,
'actions' => array('name' => 'Test', 'link' => 'Link')
);
try {
$access_token = $facebook->getAccessToken(); // returns valid access token
$uid = $facebook->getUser(); // always return 0
$result = $facebook->api( '/' . $_config['fb_profile'] . '/feed/', 'post', $attachment); // $_config['fb_profile'] procudes 'me' in this case
} catch (FacebookApiException $e) {
echo $e->getMessage();
}
}
Just to note: I am not working on local environment.
problem solve it as asked in scope request to Facebook for authentication, as I decided to use the JavaScript SDK-and then, here's the solution:
FB.getLoginStatus(function(response) {
if ( ! response.authResponse ) {
FB.login(function(response) {
// handle the response if needed
}, {scope: 'publish_actions, publish_stream'});
}
});
Thank you! :-)
I try to post on my wall using facebook api, and offline access tokens.
And every time I has one mistake:
Uncaught OAuthException: (#200) The user hasn't authorized the
application to perform this action
Here is my code:
require 'api/facebook.php';
$facebook = new Facebook(array(
'appId' => "app_id",
'secret' => "app_sec",
"cookie" => true,
'fileUpload' => true
));
$facebook->setFileUploadSupport(true);
$access_token = $facebook->getAccessToken();
$user_id = $facebook->getUser();
$result = mysql_query("UPDATE users SET user_id_facebook='".$user_id."' WHERE id='".$myrow2['id']."'",$db);
$result = mysql_query("UPDATE users SET access_token_facebook='".$access_token."' WHERE id='".$myrow2['id']."'",$db);
if($user_id == 0 || $user_id == "")
{
$login_url = $facebook->getLoginUrl(array(
'redirect_uri' => "http://apps.facebook.com/rapid-apps/",
'scope' => "email,publish_stream,user_hometown,user_location,user_photos,friends_photos,
user_photo_video_tags,friends_photo_video_tags,user_videos,video_upload,friends_videos,offline_access"));
echo "<script type='text/javascript'>top.location.href = '$login_url';</script>";
exit();
}
$post = array(
'access_token' => $access_token,
'message' => 'This message is posted with access token - '
);
$res = $facebook->api('/me/feed', 'POST', $post);
as of may 2nd the offline_access permission will be deprecated and should not be used anymore
in the meantime you have an option to disable this deprecation through the developers site for your application ( https://developers.facebook.com/apps/330955886953999 )...
for further information about the removal see:
http://developers.facebook.com/roadmap/offline-access-removal/
You need to adjust the permissions in your initial app authorization request sent to the user. By default you only gain read-only access to their basic information.
See http://developers.facebook.com/docs/authentication/permissions/
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;
}
?>