I searched for an hour before posting this question so please forgive me in advance if this is a basic, dense question. I feel like there must be something simple I'm not wrapping my head around.
I see many apps, e.g. "WeForPresident" which when joined, post a simple feed. It contains no special formatting, just an image, a link to an external site and some text.
I cannot find a way to do this with the graph api. When I post using the link param, I get 'shared a link' formatting, which is undesired. And yet, using the message param does not allow linking.
So, how are apps such as WeForPresident achieving this effect?
Thanks again.
Bounty Info
Using /TEST_USER_ID/feed with message, link, description, picture, name
Posts as a link
With actions param added
And the application in question the user talks about
Notice no Share mentioned anywhere in the post
Are you talking about something like this?
That's possible through Open Graph Objects and Actions. Here's a brief (and hopefully not terribly confusing) breakdown:
Object: In the image above, the object is "pet who needs a home". An object is text to describe the page you happen to be sharing, and is referenced in the header tags of the page you're sharing (more on that soon.)
Action: In the above image, refers to "shared" in the "... shared a pet who needs a home...". Actions are whatever it is that you're actually doing with open graph. I've seen sites "sign a petition on so-and-so" or "play such-and-such a word on this-and-that app". The bolded words are the action.
Both actions and object types can be defined on http://developers.facebook.com. If / when you have an app, and you opt to use Open Graph, it encourages you to create at least one action and one object type. There are a number of predefined ones ("reading a book", "cooking a recipe", "watching a video", etc...). Actions and objects help you personalize the messages you post to people's walls.
It's worth noting that if you define a custom Action, Facebook needs to approve it before they're willing to let just anyone use it. Normally it's not terribly difficult...you just have to click on a "Submit" button next to the Action and it will tell you if / when it gets approved.
So now we've gotten the basics out of the way, let's talk about how to implement something like this on your pages. As I'm sure you're well aware, Open Graph makes use of FB.api() instead of FB.ui(). I'm going to use a Javascript example here.
FB.api(
'/me/app_namespace:share',
'post',
{
pet_who_needs_a_home: document.location.href,
image: pet_image // optional
},
function(response) {
// You can do something with the response here.
// If successful, Facebook returns the post id of the post it just made
// If it fails, check response.error
}
);
There's also two very important tags that you need in your <head> tag. And here they are:
<meta property="fb:app_id" content="YOUR_APP_ID" />
<meta property="og:type" content="APP_NAMESPACE:pet_who_needs_a_home" />
Now let me talk you through what everything is. FB.api() is rather obvious so I'll skip onto...
'/me/app_namespace:share',
/me is posting to your wall. app_namespace is the namespace found in your app details section on developers.facebook.com. You may need to define a namespace (settings->basic->second text box). the ":" breaks the namespace and action. Share is the action (defined, again, on developers.facebook.com).
'post',
Tells Facebook that we want to use a POST request (as opposed to a GET request).
{
pet_who_needs_a_home: document.location.href,
image: pet_image // optional
},
This is (obviously) a javascript array of two important values. "pet_who_needs_a_home" is the (actually my...you should replace that with whatever yours is) object type (still on developers.facebook.com) which, as you recall, helps us define the language used, like in the image above. Image is an optional field in which you can define the image that will be shared through open graph. There are a slew of other optional fields that you can check at...you know.
function(response) {
...and all that is kind of obvious so I'll spare the pointless details there. As I mentioned in the comments, it returns either the post id of the post if successful, or an error (found in response.error) that may or may not be descriptive.
The meta tags, I hope, speak for themselves. YOUR_APP_ID is...you guessed it...your app ID. All numeric. app_namespace is your namespace, again. pet_who_needs_a_home is (my) object and should be replaced with whatever object you happen to be using.
Sorry for the long post. Hopefully that sort of cleared that up a little bit.
this will post the exact same post as of weForPresident via your appName. please change your messages accordingly. and access token should have publish_actions extended permission
$attachment = array(
'access_token' => $access_token,
'message' => "I Just Join WeForPresident",
'name' => "WeForPresident",
'description' => "philippe harewood just started gamng the election on weForPresident and could possibly win some cool stuff",
'link' => "any link to external site",
'picture' => "image link to display in left box"
);
$facebook->api("me/feed","POST",$attachment);
hope it helps.
update: javascript dialog for above solution will be something like below, which will prompt user to post and so will not need publish_actions extended permission:
<html xmlns='http://www.w3.org/1999/xhtml'
xmlns:fb='https://www.facebook.com/2008/fbml'>
<head>
<title>My Feed Dialog Page</title>
</head>
<body>
<div id='fb-root'></div>
<script src='http://connect.facebook.net/en_US/all.js'></script>
<p id='msg'></p>
<script>
FB.init({appId: '#############', status: true, cookie: true});
// calling the API ...
var obj = {
method: 'feed',
link: 'any external url',
picture: 'picture url to show up in left',
name: 'name of the post: comes as heading in blue color within box',
description: 'post description, that comes within box'
};
function callback(response) {
}
FB.ui(obj, callback);
</script>
</body>
</html>";
complete code for such post : first download the php-sdk from https://github.com/facebook/facebook-php-sdk
<?php
include_once "facebook-sdk/facebook.php";
$facebook = new Facebook(array(
'appId' => APP_ID,
'secret' => APP_SECRET,
));
if(isset($_GET['code']))
{
$access_token = $facebook->getAccessTokenFromCode($_GET['code'],$_SESSION['redirect_uri']);
$facebook->setAccessToken($access_token);
$attachment = array(
'access_token' => $access_token,
'message' => "$message",
'name' => "$name",
'description' => "$description",
'link' => "any external link",
'picture' => "picture url"
);
$facebook->api("me/feed","POST",$attachment);
}
else
{
$scope = "publish_stream";
$params = array('scope' => $scope);
$loginUrl = $facebook->getLoginUrl($params);
echo("<script> top.location.href='" . $loginUrl . "'</script>");
}
?>
This ended up being a bug which is not fixed and has a current state of "triaged"
http://developers.facebook.com/bugs/138798476259001
The only solution so far seems to be adding the action paramaters
Related
I'm new to Facebook APIs and have to do something which seems to me like it should be fairly simple, but there are so many ways to turn I'm looking for someone who knows the terrain to point me in the right direction. (I may already be pointed in it, but I'm not having success so I'm asking for help.)
I have a client website which has the Like button on certain pages (along with much other content of course). That is already implemented using an iframe tag, but I can do it a different way if need be.
What is not implemented and needs to be is that if the visitor has Liked that page, I want to show one block of content, and if he hasn't I want to show a different block. Most of the page would be identical, so I can't redirect or change the URL to accomplish this. Nor can I prompt the user for permission. I have to silently determine whether the visitor Likes the page he's currently on.
Ideally, what I'd like is a php function that will return a boolean whether the visitor likes the current URL or not. Here it is in pseudocode:
function VisitorLikesPageInFacebook($url) {
# $url = the URL to this page (NOT a Facebook page)
# if visitor is not logged into Facebook, return false
# if visitor is logged into Facebook and likes $url, return true
# if visitor is logged into Facebook and does not like $url, return false
# if anything else happens, return false
}
Can this be done using php? Do I need the Facebook PHP SDK? What settings do I need to have in my Facebook app, or better is there any possible way I can do this without creating a Facebook app? I've tried using $facebook->getUser() but it returns 0 unless I first go to the $facebook->getLoginUrl() address and give permission, which I can't expect visitors to the client site to do. I've tried $facebook->getSignedRequest() but it returns NULL. Is there a setting in my Facebook app that I need to set, or some other call I need to make first? Will I or my client need to have https to give the Facebook app a secure location?
The other piece of this puzzle is that I want the page to refresh when the Like button on it is clicked, and presumably when it does the return value of the above function will change. For that I assume I'll need to use the JavaScript SDK and subscribe to the Like event. Is that right? (My fallback approach, if I can't do what I need to via php, is to use the JavaScript SDK, subscribe to the Like event, and set or unset a cookie depending on whether he Liked or Unliked. Then read the cookie with php. JavaScript is nowhere near as familiar to me as php, so I'll avoid it except where required.)
If anyone can point me in the right direction, I'd be very happy! Thanks in advance.
$pageID= 'YOURID'; // Used below to fetch
$config = array(
'appID' => 'UR_ID',
'secret' => 'UR_SECRET',
'allowSignedRequest' => false,
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<html>
<head><title>FB TEST</title></head>
<body>
<?php
if($user_id)
try {
$user_like = $facebook->api('/'.$user_id.'/likes/'.$pageID,'GET');
if(!$user_like){
echo 'User doesnt like';
} catch(FacebookApiException $e) {
$login_url = $facebook->getLoginUrl();
echo 'Please auth us here.';
error_log($e->getType());
error_log($e->getMessage());
}
I have a web-based news app that runs on Heroku. When users post a comment to a news story on my app, my app forwards the comment to the user's facebook wall using fb_graph. Everything worked perfectly until a couple of weeks ago. For no reason that I can explain I am now seeing some baffling behavior.
Now, when a user submits a comment to a story the FB API responds with, OAuthException :: (#1500) The url you supplied is invalid. If, the same user then submits additional comments to the same story those comments are posted to the user's FB feed just fine.
I have used the FB Graph API explorer to confirm that I have valid access tokens, and that my app does accept posts to the token-owner's FB feed.
To make things even more baffling, when running my web app in development on localhost all of the posts go through just fine to my development FB app.
def post_to_facebook(story, post)
auth = Authentication.find_by_provider_and_user_id("facebook", current_user.id)
if auth
me = FbGraph::User.me(auth.token)
if me.permissions.include?(:publish_stream)
begin
me.feed!(
:message => "#{best_name(current_user)} made the following post to NewsWick: #{post.contents}",
:name => story.title,
:link => "https://www.newswick.com/stories/"+story.id.to_s,
:description => "Story posted to the NewsWick world-wide news service" ,
:picture => best_photo(story)[:photo_url]
)
rescue => e
#msg = "Facebook posting error: "+ e.to_s
puts "Facebook feed posting error: #{e.message}"
end
else
#msg = "No longer authorized to post to Facebook."
end
end
return #msg
end
One last thing to note, the only thing that I have changed w/r/t how my app interacts with FB in the last two weeks was that i accepted FB's July Breaking Changes.
Anyone have any clues. This is driving me bonkers!!!
I'm having the same issue only difference is I'm using the javascript api.
Seems like it's a facebook bug, which is already reported here: https://developers.facebook.com/bugs/136768399829531
Yes this is a known bug and Facebook developers are looking into it, well so they claim,however something interesting I found out is:
I post to my Facebook using 2 methods using RestFB API , first, for messages with URLs e.g www.something.com and those without URLs, I realized last night that all posts without URL worked and the ones with URL doesn't.
So I changed all my implementation to send messages to Facebook without using with link parameters for all posts, with or without links.
With link Parameter - throws error #1500
FacebookType publishMessageResponse = resftFBclient.publish(FACEBOOK_PAGE_ID
+"/feed", FacebookType.class, Parameter.with("message", "Hello StackOverFlow!"),
Parameter.with("link", "message with a link , www.me.com"));
With no link parameter - this works even if message contained URL/link
FacebookType publishMessageResponse = resftFBclient.publish(FACEBOOK_PAGE_ID. +
"/feed",FacebookType.class,Parameter.with("message", "My message"));
This works even if message contained URL/link and it creates an clickable link on FB. Could it be that FB is trying to drop the link implementation and letting us figure it out that the former works just as the link implementation? What's the difference anyways?
That's brutal!
Cheers
Babajide
I was trying to solve this problem this problem that seems to be occurring to almost everyone.
I am using the PHP SDK.
What I noticed is that it always returned this error for the first time I tried to post the link. On a second try, it was posted with success.
Really hackishly I then checked for an error and retried to post to the wall.
$errorCount = 0;
function postPicture($phrase)
{
try
{
$image = $_SESSION['photoLink'];
$facebook->setFileUploadSupport(true);
$response = $facebook->api(
'/me/feed',
'post',
array(
'message' => $phrase,
'picture' => 'http://mylink/pictures/facebook.png',
'link' => $image,
'caption' => 'My caption',
'description' => 'My description',
'type' => 'photo',
'name' => 'My name'
)
);
echo 'Success';
}
}
catch (FacebookApiException $e)
{
// You really should check if this $error is #1500 before doing that. I didn't :)
if($errorCount < 2)
{
postPicture($phrase);
$errorCount++;
}
else
{
$e = str_replace('"', "", $e);
$e = str_replace("'", "", $e);
echo 'Error ' . $e;
}
}
}
To solve these problems just make sure you add these og metadata tags in the head section of the page represented by the url you want to share:
<meta property="og:type" content="article" /> //or any other type like blog, website etc....
<meta property="og:url" content="your article url here" />
<meta property="og:title" content="your article title here" />
Good luck!
When posting to /me/feed, it's possible to add a page id of location with "place". This results in a post as a check in that shows the message with a map of the location.
Adding "place" as a query string parameter to the feed form at facebook.com/dialog/feed doesn't seem to add the location to the post. Is there a way to use /dialog/feed and add a location?
Please, provide your code.
This is my code, and it's working:
$post_url = '/' . $user . '/feed';
$msg_body = array(
'message' => 'message for user timeline',
'place' => '106339232734991',
);
// posting on user page feed
$postResult = $facebook->api($post_url, 'post', $msg_body );
After discussing on FB group, it looks like only message, picture, and place work to make a checkin with a location. If adding other parameters like caption or link, it turns into a regular feed post without a location. This is unfortunate because without a link back to the app, there is almost no incentive to make a checkin from an app.
https://developers.facebook.com/bugs/428651263853608 - “javascript ui feed dialog - place parameter does not work”
Response from Facebook:
'place' is not a supported parameter per https://developers.facebook.com/docs/reference/dialogs/feed/.
Status changed to By Design
I have a question, It might be found somewhere but I couldnt find a thread for it so if there is one, please post the link.
I admin a page that is a group of several other business areas. I want to post on one page with different icons and names, can that be done? Like different admins but administered by one person/account?
Possible?
What you are going to want to do is use a "Page access_token". To get this you will have to create an application and grant it the manage_pages permission.
You should see in the Authentication Documentation a section called "Page Login".
You can grant your application the manage_pages permission by going to this URL:
https://www.facebook.com/dialog/oauth?client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&scope=manage_pages&response_type=token
Don't forget to substitute YOUR_APP_ID and YOUR_URL with the correct values for you application and URL. (the URL can be any URL - it is where Facebook will send you after you close the Dialog). You will see a dialog that looks something like this :
Once you have the correct permission, you'll want to make a call to this URL :
https://graph.facebook.com/me/accounts?access_token=TOKEN_FROM_ABOVE
You will get a response similar to :
As you can see from the image, you will receive a list of all the pages that the user administers along with an access_token for each page.
You use this access_token to make posts on behalf of the page. Since you have not stated what programming language you are using I will give an example in php.
In php, posting to the page would look something like this :
$facebook->setAccessToken(ACCESS_TOKEN_YOU_RETRIEVED_EARLIER);
$attachment = array('message' => 'this is my message',
'name' => 'This is my demo Facebook application!',
'caption' => "Caption of the Post",
'link' => 'http://mylink.com',
'description' => 'this is a description',
'picture' => 'http://mysite.com/pic.gif',
'actions' => array(array('name' => 'Get Search',
'link' => 'http://www.google.com'))
);
$result = $facebook->api('/PAGE_ID/','post',$attachment);
Hope this helps!
Happy coding!
I have an Facebook App that is currently posting to a WALL. All the posting are being Titled as MY post. I would like it to be titled by the App. In this documentation, it says there is a 'from' parameter. I've tried to use it a couple of ways and both ways have been unsuccessful. Does anyone have a clue?
https://developers.facebook.com/docs/reference/dialogs/feed/
$attachment = array('message' => $message,
'from' => "<APP_ID>",
//'from' => array('name' => "Sender's Name",'id' => "<APP_ID>"),
}
$me = $this->facebook->api('/me/feed/', 'POST', $attachment);
The from property MUST be a user or a page. You could possibly create a Facebook page with the name of your application and use that.
I've never used the FROM parameter in the Graph API, just on the legacy REST API. And that API parameter was actually actor_id instead of FROM.
Obviously this doesn't solve your problem. To solve your problem, get the access_token for your page from the /me/accounts Graph API end point. When you use this access token, you are identified as the account for which it corresponds, and when you post to /{page_id}/feed with it, it will be posted as the page and in turn be published out to all your users.
<script type="text/javascript">
FB.api("/123123123/feed", "post", {
message: "Test!",
access_token: '184484190795|SDlfkjweifljasdf.3600.123123123.1-2342342|234234234234|XLjsldfjLISJflwieab'
},
function(response)
{
console.log(response);
});
</script>
Thank you all for the suggestions. I finally was able to solve the problem by combining these 2 articles. The basic steps of the first article is still valid. But it is written in the old API. The second article help be get through the step the new REST api couldn't do. Hope this helps.
http://www.emcro.com/blog/2009/01/facebook-infinite-session-keys-no-more/
http://www.alyssapowell.com/2010/10/how-to-generate-an-infinite-session-key-in-facebook/