How do I use Perl's WWW::Facebook::API to publish to a user's newsfeed? - perl

We use Facebook Connect on our site in conjunction with the WWW::Facebook::API CPAN module to publish to our users newsfeed when requested by the user.
So far we've been able to successfully update the user's status using the following code:
use WWW::Facebook::API;
my $facebook = WWW::Facebook::API->new(
desktop => 0,
api_key => $fb_api_key,
secret => $fb_secret,
session_key => $query->cookie($fb_api_key.'_session_key'),
session_expires => $query->cookie($fb_api_key.'_expires'),
session_uid => $query->cookie($fb_api_key.'_user')
);
my $response = $facebook->stream->publish(
message => qq|Test status message|,
);
However, when we try to update the code above so we can publish newsfeed stories that include attachments and action links as specified in the Facebook API documentation for Stream.Publish, we have tried about 100 different ways without any success.
According to the CPAN documentation all we should have to do is update our code to something like the following and pass the attachments & action links appropriately which doesn't seem to work:
my $response = $facebook->stream->publish(
message => qq|Test status message|,
attachment => $json,
action_links => [#links],
);
For example, we are passing the above arguments as follows:
$json = qq|{ 'name': 'i\'m bursting with joy', 'href': ' http://bit.ly/187gO1', 'caption': '{*actor*} rated the lolcat 5 stars', 'description': 'a funny looking cat', 'properties': { 'category': { 'text': 'humor', 'href': 'http://bit.ly/KYbaN'}, 'ratings': '5 stars' }, 'media': [{ 'type': 'image', 'src': 'http://icanhascheezburger.files.wordpress.com/2009/03/funny-pictures-your-cat-is-bursting-with-joy1.jpg', 'href': 'http://bit.ly/187gO1'}] }|;
#links = ["{'text':'Link 1', 'href':'http://www.link1.com'}","{'text':'Link 2', 'href':'http://www.link2.com'}"];
The above, nor any of the other representations we tried seem to work. I'm hoping some other perl developer out there has this working and can explain how to create the attachment and action_links variables appropriately in Perl for posting to the Facebook news feed through WWW::Facebook::API.
Thanks in advance for your help!

I think the problem is that your JSON string might be invalid. I was able to get it to work by just using JSON::Any to serialize a Perl data structure instead of building the JSON string manually. (WWW::Facebook::API uses JSON::Any under the hood; it would be nice if it could take a Perl data structure instead of a JSON string. I will try to submit a patch this weekend.)
use WWW::Facebook::API;
use JSON::Any;
my $j = JSON::Any->new;
my $fb = WWW::Facebook::API->new(
desktop => 0,
api_key => $api_key,
secret => $secret,
session_key => $session,
session_expires => $expires,
session_uid => $fb_uid
);
my $res = $fb->stream->publish(
message => 'Test message',
attachment => $j->objToJson(
{ name => 'Foo bar baz',
href => 'http://www.google.com/',
description => "this is a thing"
} ),
action_links => $j->objToJson(
[ { text => 'action link text',
href => 'http://www.foobar.com/'
} ] )
);
The result:
http://www.friedo.com/fb_attach.jpg

Related

facebook api: remove the "shared a link" when posting via api

I think there is a trick to remove the "shared a link" next the username when you post something via the facebook api. I notice it's removed when you add a message field to your post, but I would like to get it even without any message. I tried to pass TRUE, a blankspace, "&nbsp", etc as message but that was like there was no any message.
array(
'message' => " ", // I guess the trick is somewhere here
'link' => $link,
'name' => $name,
'description' => ' ',
'caption' => $date,
)
what I would like:
what I get:
with a message:

Posting picture to Facebook wall via POST object not appearing

I'm trying to post a picture with publish_stream permissions from my site onto Facebook. For some reason, the picture doesn't show up when I post but everything else works fine.
By accessing http://www.mysite.com/image/questions.png, the picture shows up correctly. Am I missing something?
$body = array(
'name' => 'Join this site',
'message' => '',
'description' => 'Check this out',
'picture' => 'http://www.mysite.com/image/questions.png',
'link' => SITE_URL,
);
$batchPost = array();
Then I batch post.
Double check to see if it really works.
$body = array(
'name' => 'Join this site',
'message' => '',
'description' => 'Check this out',
'picture' => 'http://www.mysite.com/image/questions.png',
'link' => SITE_URL,
);
$batchPost = array();
$status = $facebook->api('/me/feed', 'POST', $body);
if (isset($status['id'])) //Add a check
{
echo "Message posted to wall!";
}
If not you can use JavaScript SDK FB.ui method to post to Facebook Wall. It is more flexible in my opinion as users can input their own message, unlike how it is defined by the developer through the PHP SDK method.
JavaScript SDK FB.ui Method
<script>
function postToWall()
{
var obj = {
method: "feed",
name: "Join this site",
description: "Check this out",
caption: "CAPTION",
picture: "http://www.mysite.com/image/questions.png",
link: "SITE_URL"
};
function callback(response) {
//do something
}
FB.ui(obj, callback);
}
</script>
More on JavaScript SDK Feed Dialog: http://developers.facebook.com/docs/reference/dialogs/feed/
Edit: Use another image and try again. http://www.mysite.com/image/questions.png has been removed. Upload your picture on some image hosting website instead of hotlinking the image directly.

how to set album privacy settings using facebook graph api

Is there any way to change the Facebook album privacy settings with graph api?
I'm trying to find out, but all I could found is how to get the privacy settings using fql, but not to set.
I'm creating the album as follow
$postdata = http_build_query(array(
'name' => $album_name,
'message' => $album_description
)
);
$opts = array('http' =>
array(
'method'=> 'POST',
'header'=>
'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
) $context = stream_context_create($opts);
$result = json_decode(file_get_contents($graph_url, false, $context));
$albumid = $result->id;
Now if I add privacy=>"value", it gives $albumid=null.
I'm not sure where I need to add privacy parameter.
When you create an album, you can send these parameters in post request.
name, message, location and privacy.
Value of privacy field can be set like this,
privacy={value: "CUSTOM"} (send this as post parameter)
The value field may specify one of the following strings:
EVERYONE, ALL_FRIENDS, NETWORKS_FRIENDS, FRIENDS_OF_FRIENDS, CUSTOM .
As facebook docs sucks, there's no mention about it on albums object page.
However, you can read it on post object.
Edit: (after comments)
In php sdk you can do something like this,
$ret_obj = $facebook->api('me/albums', 'POST',
array(
'privacy' => '{value: "CUSTOM"}',
'location' => 'India'
));
The document of creating an album is put in https://developers.facebook.com/docs/reference/api/user/#albums
Privacy setting is a json-style string. So you could create an array() and use json_encode() to generate it.
with php sdk it is also possible as php style #kaur
$ret_obj = $facebook->api('/me/albums/', 'POST', array(
'source' => '#' . $photo,
'message' => 'Picture uploaded',
'location' => 'Goran',
'privacy'=> array('value'=>'EVERYONE'), //'privacy'=> '{value: "EVERYONE"}', //worked too!! SELF, ALL_FRIENDS, EVERYONE
)
);

How distinguish between pages and applications?

Using php-sdk I obtain pages in this way:
$array = $this->instance->api(
'/' . $fb_user . '/accounts', 'GET', array('access_token' => $access_token)
);
Now var_dump($array['data']); gives me:
array (
0 =>
array (
'name' => 'my name 1',
'access_token' => '***',
'category' => 'Electronics',
'id' => '***',
),
1 =>
array (
'name' => 'my name 2',
'access_token' => '***',
'category' => 'Application',
'id' => '***',
)
Only the first item is a page, the second is an app. Any way to distinguish them or improve this snippet?
EDIT: Using the category field seems a too weak procedure.
If you don't want to rely on "category", you need to fetch informations from these ids.
Run a query for:
https://graph.facebook.com/[page_or_app_id]
Among other things, there will be field:
{
[...]
"type": "application"
]
or:
{
[...]
"type": "page"
]
To avoid long responses, you may consider Batch Requests as described:
http://developers.facebook.com/docs/reference/api/batch/ (general instruction)
Batch calls with Facebook Graph API & PHP (php examples)
Give that the category says "Application", I'd use the category property to filter with.
Alternatively, if as you say using category is too weak for you. You can use strong FQL. SELECT display_name FROM application WHERE app_id='{the id from your me/accounts}' and see if you get back an object or not.

How to include html in stream.Publish

I'm using the old Rest API (and old Javascript SDK) to develop an iframe application inside facebook.
However I would like to have the wall posts (calling stream.Publish) to include new lines and having people's names with links to their profiles. However every time I include html content, FB strips it.. but I know it can be done, because some apps do it, for example:
http://img.skitch.com/20100702-jhqradpi3td4d53sdb3qin92sb.png
Cheers,
Ze
You cannot have arbitrary HTML in a wall post. If it looks like HTML, Facebook will strip it. The only other alteration Facebook will do I believe is convert text that looks like links into links (so if you have http://www.google.com somewhere in the message, Facebook will automatically turn it into a link).
However Facebook does provide basic facilities for including basic things like a picture, caption, description, link, etc through stream.publish by passing in additional parameters. This is an example for including a few of these things from Facebook's documentation (http://wiki.developers.facebook.com/index.php/Stream.publish):
$message = 'Check out this cute pic.';
$attachment = array(
'name' => 'i\'m bursting with joy',
'href' => 'http://icanhascheezburger.com/2009/04/22/funny-pictures-bursting-with-joy/',
'caption' => '{*actor*} rated the lolcat 5 stars',
'description' => 'a funny looking cat',
'properties' => array('category' => array(
'text' => 'humor',
'href' => 'http://www.icanhascheezburger.com/category/humor'),
'ratings' => '5 stars'),
'media' => array(array('type' => 'image',
'src' => 'http://icanhascheezburger.files.wordpress.com/2009/03/funny-pictures-your-cat-is-bursting-with-joy1.jpg',
'href' => 'http://icanhascheezburger.com/2009/04/22/funny-pictures-bursting-with-joy/')),
'latitude' => '41.4', //Let's add some custom metadata in the form of key/value pairs
'longitude' => '2.19');
$action_links = array(
array('text' => 'Recaption this',
'href' => 'http://mine.icanhascheezburger.com/default.aspx?tiid=1192742&recap=1#step2'));
$attachment = json_encode($attachment);
$action_links = json_encode($action_links);
$facebook->api_client->stream_publish($message, $attachment, $action_links);
Check this out for more info on what attachments you can include.