Tuesday, 1 October 2013

Facebook image upload with custom post message

Facebook image upload with custom post message

I am currently building an app which generates a custom image with user's
facebook username on it. The username is being collected by one of the
following methods:
Asking user for his Facebook username and doing a graph search and thus
obtaining his username.
Threw Facebook connect which will authorize user to my website and I can
get what I want (with Permissions: publish_stream,user_photos )
In the end, I let user to post the image to his Facebook wall threw a button:
<a href="imageupload.php?filename="abc" >Post this image to facebook</a>
and content of imageupload.php is:
$appId = 'MY_APP_ID';
$appSecret = 'MY_APP_SECRET';
$return_url = 'MY_HOME_URL';
$fbPermissions = 'publish_stream,user_photos';
include_once("inc/facebook.php"); //include facebook api library
//Call Facebook API
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $appSecret,
'fileUpload' => true,
'cookie' => true
));
$facebook->setFileUploadSupport(true);
//get user
$fbuser = $facebook->getUser();
$access_token = $facebook->getAccessToken();
//get full image path else logout
if(!empty($_GET['filename'])){ $img = 'uploads/'.$_GET['filename'].'.jpg';}
else{
$loginUrl = $facebook->getLoginUrl(array('scope'=>$fbPermissions,
'return_url'=>$return_url));
header('Location: ' . $loginUrl);
}
//variables we are going to post to facebook
$msg_body = array(
'source' => '@' . realpath($img),
'message' => 'I have created this image at '.$return_url
);
if ($fbuser){ //user is logged in to facebook, post our image
try {
$uploadPhoto = $facebook->api('/me/photos', 'POST', $msg_body );
} catch (FacebookApiException $e) {
echo $e->getMessage(); //output any error
}
}else{
$loginUrl =
$facebook->getLoginUrl(array('scope'=>$fbPermissions,'return_url'=>$return_url));
header('Location: ' . $loginUrl);
}
This successfully post my image to Facebook with a "PREDEFINED MESSAGE". I
know I have authorized user, but wouldn't that be counted in a Black Hat
spamming if you are self generating a message on user's wall without
letting him to post the text if he wanted to.
If so, How can I use some dialog box like Facebook Feed (Which I am not
using BTW because it is not the way to post image I suppose) that can
"ethically" send user's photo with a custom message(if this is considered
ethical) to his/her Facebook's wall?

1 comment: