Can anyone help me to send message to facebook friends using graph api.
I tried
$response = $facebook->call_api("/me/feed", "post", "to=john","message=You have a Test message");
It's not working.
I have the accesstoken of the user in my hand.only I am confused on sending process.
You can't send messages using a Facebook application. You used to be able to do that, but the (predictable?) colossal amount of abuse led to the revocation of this ability.
Provided Alice, your user, has given you the necessary extended permissions, you have the following options:
Post to Alice's wall on her behalf
Send email to Alice
Create events on behalf of Alice
invite Bob (not your user) to said events
Issue a request/invitation on behalf of Alice to Bob
Issue a request from the App to Alice
You could open the Send Dialog in a popup.
$parameters = array(
'app_id' => $facebook->getAppId(),
'to' => $facebookUserId,
'link' => 'http://google.nl/',
'redirect_uri' => 'http://my.app.url/callback'
);
$url = 'http://www.facebook.com/dialog/send?'.http_build_query($parameters);
echo '<script type="text/javascript">window.open('.json_encode($url).', ...
For detailed options see:
https://developers.facebook.com/docs/reference/dialogs/send/
$attachment = array(
'access_token' => $access_token,
'message' => $msg,
'name' => $name,
'link' => $link,
'description' => $desc,
);
facebook->api('/'.$uesr_id.'/feed', 'POST', $attachment);
Technically you can do feed or cross feed post with privacy settings that allows only the feed owner to see the post but its not really sending a message to a person.
You can use
HTTP POST with
PATH
https://graph.facebook.com/friend_facebook_id/feed
PARAMETER
MESSAGE = your message
ACCESS_TOKEN = your oauth2 access token
You can send to their facebook email. Facebook email is consisting profile nickname+'#facebook.com'. The email will goes to their facebook inbox message. Note that facebook email does not accept spoofing email. You will need whitelabel domain or use SendGrid.
You will need to integrate xmpp chat to reply a message and to write a new message.
I saw this post and noticed it was not right. Using the javascriot api you can post to a friend's feed like so:
In this example "friendID" is the FB user ID of the friend.
This api call requires the "publish_stream" permission.
FB.api('/'+friendID+'/feed', 'post',
{
method: 'feed',
message: messageText,
name: 'write a title here',
caption: 'Put a caption here.',
description: 'Put your description here.',
link: 'http://stackoverflow.com/questions/2943297/how-send-message-facebook-friend-through-graph-api-using-accessstoken',
picture: 'link to the preview thumbnail',
},
function(response) {
if (!response || response.error) {
//alert('Error occured');
} else {
//alert('Post ID: ' + response.id);
}
});
So this does it with the javasfcript SDK- the PHP method must be similar.
Instead of using the below code
[facebook dialog:#"feed"
andParams:params
andDelegate:self];
Use the following solution
[facebook requestWithGraphPath:#"me/feed"
andParams:params
andHttpMethod:#"POST"
andDelegate:self];
Related
I an facing an issue with the Facebook SDK POST action when posting a very basic message on my page which is linked to my Twitter account. I have tried to solve the issue by myself using Google and StackOverflow, but I didn't manage to find a fix.
The issue is the following:
When I post a message with the SDK:
It appears in the page and is visibe by myself
It appears in the page also for other Facebook user
It appears in the Facebook Feed
but the message is not transmitted to my Twitter account
When it post something "manually" it is correctly sent to my Twitter account. The issue only appears when using the SDK. Hereafter you can find my code and the context of my request:
I am using a PHP script on my personal desktop (WAMP server).
The attachement is very very basic: 'message' => "Hello dear followers!"
The application used to post is not in sandbox mode.
Your app detail page has not yet been submitted for review.
The message appears as public on the page and is indeed visible by other users.
When I post manually the exact same message, it is correctly sent to Twitter.
Can you help me with this issue? Many thanks and all the best.
$appid = *(hidden)*;
$appsecret = *(hidden)*;
$pageId= *(hidden)*;
$facebook = new Facebook(array('appId' => $appid,'secret' => $appsecret,'cookie' => false));
$user = $facebook->getUser();
if($user){
try{
$page_info = $facebook->api("/$pageId?fields=access_token");
if (!empty($page_info['access_token'])){
$attachment = array(
'access_token' => $page_info['access_token'],
'message' => "Hello dear followers!",
'actions' => json_encode(array('name' => $action_name,'link' => $action_link))
);
$status = $facebook->api("/$pageId/feed", "post", $attachment);
}
}
}
The issue appears because of the special action item:
'actions' => json_encode(array('name' => $action_name,'link' => $action_link))
Removing this specificity the message is transmitted to Twitter, successfully.
My question is very simple and has been answered lots of times in Stackoverflow but in order to the changing of the Facebook API I want to make certain if a user who is using my app can post to their wall a message generated automatically by the app.
If the user is reading in my app a comment he/she likes, I want he/she can post to their wall a message like "I've been reading a very interesting comment about..." and I would like that tis text appears automatically in the window where Facebook requests permission to the user to post in their wall.
I've tried the JS API with "feed" method but the text in the field message is ignored. I'm going to try with PHP API but I think the result will be the same.
Thanks in advance!!
JavaScript SDK
You can send the following values using the JavaScript SDK:
// calling the API ...
var obj = {
method: 'feed',
link: 'https://developers.facebook.com/docs/reference/dialogs/',
picture: 'http://fbrell.com/f8.jpg',
name: 'Facebook Dialogs',
caption: 'Reference Documentation',
description: 'Using Dialogs to interact with users.'
};
(code and image taken from the docs)
I described, what field is what text in the popup.
If not provided, name, image, caption and description are derived from the webpage, given in the link.
You cannot include a default message in the JS popup.
PHP SDK
If you are using the PHP SDK, you can however include a message.
Code example:
try {
$facebook->api(
"/{$facebookId}/feed",
"POST",
array(
// this is the important part
'message' => "This is your message!",
'link' => $yourLink,
'name' => "This is your name",
'caption' => '..',
'description' => '...',
'type' => 'link',
'application' => array(
'name' => 'Name of your app',
'id' => $idOfYourApp
)
)
);
}
catch (\FacebookApiException $exception)
{
// ...
}
the "message" field in feed no longer works. You have to use "description" or "caption"
This is because Facebook mentioned something along the lines of "A user should be able to say what they want about content on the internet".
How can I add information about read article on my site to facebook RECENT ACTIVITY (like the Guardian)?
I use php and code:
$ret_obj = $facebook->api('/me/feed', 'POST',
array(
'link' => 'example.com',
'message' => 'Test message'
));
This code posting the message in user's timeline but I want to post message in his recent activity.
probably you wan't to use open graph api, not graph api
https://developers.facebook.com/docs/opengraph/
https://developers.facebook.com/docs/opengraph/tutorial/
https://developers.facebook.com/docs/opengraph/opengraph-approval/
I have a website, and I need it to post status updates to a Facebook Page from time to time.
Using my personal Facebook account, I created an App, and a Page. So far, I've been able to programmatically post to my Page's Wall, by adding this code to my website:
include_once "lib/facebook/src/facebook.php";
$facebook = new Facebook(array('appId' => 'APP_ID_HERE', 'secret' => 'APP_SECRET_HERE'));
if($facebook->getUser()) {
try {
$ret_obj = $facebook->api('/FACEBOOK_PAGE_ID_HERE/feed', 'POST', array(
'link' => 'www.example.com',
'message' => 'Posting with the PHP SDK!',
'access_token' => 'FACEBOOK_PAGE_ACCESS_TOKEN_HERE'
));
echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';
} catch(FacebookApiException $e) {
// user logged out (has user_id, but invalid access token)
$login_url = $facebook->getLoginUrl(array('scope' => 'publish_stream'));
echo 'Please login.';
}
echo '<br />logout';
} else {
$login_url = $facebook->getLoginUrl(array('scope' => 'publish_stream'));
echo 'Please login.';
}
So I just open my website, click "Please login", login as myself. Once I'm logged it, it will now be able to post the status update to the Facebook Page.
Obviously, the problem here is that I need to be logged in for it to be able to post. If other users will try to login with their user accounts, my website cannot post status updates to the Facebook Page because I am the only Admin for the app/Page.
My question is, is there a way for me to programmatically log myself into Facebook so I can do these status updates to my Page automatically?
Sorry, total noob here to Facebook development.
Seems like you need to update status while you are offline from facebook. To do this you need to get the offline access permission of the app user and you need to have a "infinite" token for facebook app to access the APIs, so that your program can update anytime you want with out logging into facebook.
You may get something you want here
http://developers.facebook.com/docs/authentication/
I'm a little late to the party here, but I thought I'd post my gist example for how to do something very similar. I added some functionality to fbconsole to make it easy to programmatically login with fbconsole.automatically_authenticate to make it much easier to access this information in a systematic way. This addition has not yet been incorporated into the master branch of fbconsole (it was just posted this morning), but it is available here in the meantime for those that are interested.
This is what I use to do,
// = SET WALL DATA HERE ===============
$attachment = array(
'access_token' => 'My Access token here',
'message' => '',
'name' => 'My Wall Post Header/Title Here',
'caption' => 'Small caption here',
'link' => 'http://www.mywebsite.org',
'description' => 'Wall Post Details Here',
'picture' => "http://www.mywebsite.org/images/logo.gif",
);
// =POST ON FACEBOOK WALL ==========================
$this->facebook->api('/me/feed', 'POST', $attachment);
, $attachment);
So include your Facebook API, Get your Facebook Access token, SET data for your wall post and call for facebook api with me/feed method.
This will make a wall post on your wall without asking for login.
make sure you are using correct Access Token.
Posting a message on friend's wall, graph API.
i have the publish_stream extended permission of the user, who is using the application.
the code workds if i want to post sth on my wall.
is there any method to post on wall or send message to all the friends of a particular user??
please help thanks!!
following is the code, but it is not working.
$friends = $facebook->api('/me/friends');
foreach($friends['data'] as $friend){
$friendsUserId = $friend['id'];
echo $friendsUserId . "</br>";
$result = $facebook->api('/$friendsUserId/feed', 'POST', array(
message' => 'Test Message' ));
print_r($result);
}
This would perhaps be the way to do it.. (Untested, but I use something similar for my app.)
$friends = $facebook->api('/me/friends');
foreach($friends['data'] as $friend) {
$facebook->api('/$friend['id']/feed', 'post', array(
'message' => 'just a test message',
'link' => 'http://site.com',
'name' => 'just a test name',
'caption' => 'just a test caption',
'description' => 'just a test description',
));
}
This is using the PHP API.
I just did a little testing, and having the permissions of the user, I could indeed post on the user's friends walls via the php api:
$facebook->api('/[FRIEND_ID]/feed', 'post', array(
'message' => 'test message',
'link' => 'http://google.com',
'name' => 'test name',
'caption' => 'test caption',
'description' => 'test long description',
));
The publish_stream permission only allows you to publish content on that particular users wall - you cannot post on the wall of all his friends.
Think about it this way - say you have not authorized an application to post anything on your facebook wall but just because one of your friends has given access to his wall - it automatically does not give the application access to publish on the wall of all his friends.
EDIT
Rahul, I tried yesterday night to login as myself and post a message on the wall of one of my friends through my app and surprisingly it worked. I was under the wrong impression that you could not do that. My apologies.
But I'm still not able to figure this out. Let me explain
1) I have my real facebook lets call it abc and a test facebook account xyz which I've added as my friend to abc
2) I login into my app using my abc faceboook account and publish a message on the wall of my friend xyz
3) Now I login to facebook.com as abc and it shows on my news feed the message that I published on xyz's wall. When I click on xyz's profile - the message shows up on his wall too. So far so good
4) Now I log out of facebook.com as abc and log back in as xyz. But now I dont see the message that was posted through the app on the wall of xyz.
I dont know if there is any kind of delay but I've waited 30 mins but still its not there. But it continues to show when I log in as abc.
I hope you understand what I'm trying to convey here.
I've used the same piece of code like yours - so can you try the above scenarios and see if you experience something similar
Thanks
Gubloo's answer is close.
Posting to a friend's wall is completely possible, but with one caveat, said friend must have also gone through the 'Permissions' dialogue at some point. Meaning, users of your application will be able to post to the wall's of other users of your application.
I'm going to give you the iPhone code, because I don't know php and imagine you'll get the gist. Just replace 'me' with the UID of whatever you are trying to post to, be a page, or a Friend's profile.
[_facebook requestWithGraphPath:#"/me/feed"
andParams:params
andHttpMethod:#"POST"
andDelegate:self];
I'm actually curious to know if this would give the user the ability to post on the wall of anyone who uses the application, and not just his friends.
$friends = $facebook->api('/me/friends');
foreach($friends['data'] as $friend) {
$facebook->api('/$friend['id']/feed', 'post', array(
'message' => 'just a test message',
'link' => 'sample link.com',
'name' => 'just a test name',
'caption' => 'just a test caption',
'description' => 'just a test description',
));
}
Above code work for me tooooo
Thanks for the post