i'm using this code to invite friends in my facebook application
$app_id = "000000000000000000000";
$canvas_page = "http://apps.facebook.com/applicationname/";
$message = "join me in this app.";
$requests_url = "https://www.facebook.com/dialog/apprequests?app_id="
. $app_id . "&redirect_uri=" . urlencode($canvas_page)
. "&message=" . $message;
if (empty($_REQUEST["request_ids"])) {
echo("<script> top.location.href='" . $requests_url . "'</script>");
every time the user call this page he can see all his friends
i need to view only the UNinvited friends.
how?
A better method would be to use the javascript SDK. You can use that to render a facebook popup with the share dialog inside...
With regard to your question, you can use filters for the apprequest method as stated here. You are looking for the app_non_users filter.
[EDIT]
If you want to filter even more - ie. don't list friends that have already been invited (but not yet accepted), you'll have to read each request_id generated and store the invited users facebook UID and place them in the exclude_ids filter. This method could be further optimized by using the javascript sdk and FB.ui() method, posting the generated request_ids using AJAX, reading the request_id server-side and returning the users UID to be added to a javascript array in order to be used in the exclude_ids filter for subsequent FB.ui() invite calls...
Related
I am trying to write a php script that can upload a daily photo to my personal facebook wall. I need this to be done at anytime and not just when I am logged in. I am wondering if it is possible to upload a photo to my wall using php only or do I need to use an app with the right permissions. If so do I have to personally install that app and give correct permissions for my page to make this possible, if it is possible, and can this be done automatically?
What I want to do:
In short I want a users twitter feed to be converted and put on an image. I have the script that automatically does this already. However I would love it if after the twitter user tweets then this calls my script and converts it to an image (again I have done this code already) but...
Now I need a script that can automatically upload this to my personal wall after the image is created. Is this possible?
Well, you can use the extended user access token to get it working for a maximum of 60 days. After that, the token will expire and then you'll have to refresh the token and that is not possible without the user's interaction.
The documentation states the same:
Even the long-lived access token will eventually expire. At any point, you can generate a new long-lived token by sending the person back to the login flow used by your web app - note that the person will not actually need to login again, they have already authorized your app, so they will immediately redirect back to your app from the login flow with a refreshed token - how this appears to the person will vary based on the type of login flow that you are using, for example if you are using the JavaScript SDK, this will take place in the background, if you are using a server-side flow, the browser will quickly redirect to the Login Dialog and then automatically and immediately back to your app again.
After doing the above you will obtain a new short-lived token and then you need to perform the same exchange for a long-lived token as above.
To get the extended token make the following call from your server:
GET /oauth/access_token?
grant_type=fb_exchange_token&
client_id={app-id}&
client_secret={app-secret}&
fb_exchange_token={short-lived-token}
Code
$app_id = APP_ID;
$app_secret = APP_SECRET;
$post_login_url = APP_URL;
$album_name = 'test';
$album_description = 'desc';
$code = $_REQUEST["code"];
//Obtain the access_token with publish_stream permission
if(empty($code))
{
$dialog_url= "http://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode($post_login_url)
. "&scope=publish_stream";
echo("<script>top.location.href='" . $dialog_url .
"'</script>");
}
else {
// access token
$token_url= "https://graph.facebook.com/oauth/"
. "access_token?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$access_token = $params['access_token']; // <---
// extended token
$extended_token_url= "https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=".$app_id."&client_secret=".$app_secret."&fb_exchange_token=".$access_token;
$response = file_get_contents($extended_token_url);
$params = null;
parse_str($response, $params);
$extended_access_token = $params['access_token']; // <---
// Upload to the photos to the album here
}
That's the complete auth process, incl the extended token. You can find many posts regarding the upload of the photo, simply use the $extended_access_token with your calls.
(In case you are not aware, Graph API Explorer is a fantastic tool of facebook to test the APIs).
Good luck!
I took this code from Facebook's documentation to get me started in learning how to get my app to post to a feed.
It works as described, in that when I go to my app's canvas URL, I am presented with a dialog where I can enter in text, click "share", and it posts into my timeline. So far, so good.
But I want to alter it so that instead of explicitly typing something in and clicking a button, a post is automatically sent to the feed, triggered be events in my PHP code.
However, and I realize this is a newbie kind of question, I can't figure out how to adjust the code to make that happen. My experiments either just break the code or end up with the same dialog.
How do I get the PHP to post a message straight into the feed, and then display the app's canvas URL immediately after (so as not to get caught in a loop of constantly reloading and posting over and over...)?
For convenience, here is the same code from the Facebook documentation:
<?php
$app_id = "YOUR_APP_ID";
$canvas_page = "YOUR_CANVAS_PAGE_URL";
$message = "Apps on Facebook.com are cool!";
$feed_url = "https://www.facebook.com/dialog/feed?app_id="
. $app_id . "&redirect_uri=" . urlencode($canvas_page)
. "&message=" . $message;
if (empty($_REQUEST["post_id"])) {
echo("<script> top.location.href='" . $feed_url . "'</script>");
} else {
echo ("Feed Post Id: " . $_REQUEST["post_id"]);
}
?>
You may achieve this by requiring publish_stream permission (see Publishing Permissions).
Once your app have that permission granted by user you may publish feed stories without showing Feed Dialog to user.
This also allow you to publish content to user's feed without need of user's active access_token and using application access_token for that purpose.
It's really easy to implement this using PHP-SDK:
$facebook = new Facebook(/*...*/);
$facebook->api('/USER_ID/feed', 'post', array(
'message'=>'Text entered by user'
));
Probably publish_actions permission can be sufficient for that task, but documentation isn't yet updated across developers site so it's safer to use publish_stream (see statuses for user object).
The publish_stream permission is a superset of publish_actions allowing everything that publish_actions allows plus more. Some of the additional publishing capabilities are:
posting to a friend's feed
posting questions
creating notes
posting content to events or groups
im developing a new facebook application and need to send Appusers frind list application invite.
Example,
my friend X logged CityVille App,
my friend is offline but he send CityVille Invite.
i ask to X you send this invite ? he said no :) Invite getting from city ville Automatically. (No fb dialog)
How can i do this.
P.S:
My App Required permissions is:
email,offline_access,publish_stream,user_photos,read_friendlists
I tried following code on my friendlist, but i dont send invitation returing error
$app_id = $this->facebook_config['appId'];
$app_secret = $this->facebook_config['secret'];
$token_url = "https://graph.facebook.com/oauth/access_token?" .
"client_id=" . $app_id .
"&client_secret=" . $app_secret .
"&grant_type=client_credentials";
$app_access_token = file_get_contents($token_url);
$user_id = 'USERID';
$apprequest_url ="https://graph.facebook.com/" .
$user_id .
"/apprequests?message='Mesaj'" .
"&data='bisi'&" .
$app_access_token . "&method=post";
$r = file_get_contents($apprequest_url);
echo $r;
error
{
"error": {
"type": "OAuthException",
"message": "(#200) All users in param ids must have accepted TOS"
}
}
As I understand you're asking about requests to use or invite users to the application rather than do something in the application. Well AFAIK the only way to do this is by emailing them. Facebook just don't allow this anymore, other that the default Facebook invite dialog.
The Facebook's API apprequests is to send request between the connected users only e.g. get this lost cow, or help me to win this battle ... etc.
Update: The proper way invite friends to connect is by using Facebook JavaScript SDK.
FB.ui({method: 'apprequests',
message: 'Plz add this, I need you to use this app desperately!',
to: 'any-friend-id'
});
I've just tested it and it allowed my brother to use it.
The error you are getting is most likely caused by your use of file_get_contents.
I would suggest using curl for this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apprequest_url);
$result = curl_exec($ch);
curl_close($ch);
Please be aware that (last time I checked) you must still prompt the user with some form of dialog before sending any app requests/invites. Doing it without a user's expressed permission is against FB's guidelines.
You can test the functionality of your request with the API explorer: http://developers.facebook.com/tools/explorer/?method=POST&path=me%2Fapprequests
It would seem that this would be a simple thing to do, but I just can't seem to find anything on how to accomplish this on the Facebook Developer forum, the API nor Google.
I'm using the PHP SDK to write a simple app that looks for a certain change regarding the current user (not to seem mysterious, it's just that this part is irrelevant).
As soon as this change is detected, I want to somehow alert the current user of the change, just not by posting it to the user's wall, this information needs to stay confidential - it needs to stay between the user and the app.
According to the Facebook API documentation, notifications are generated automatically - so I have no control over them, and I can't seem to get the Bookmark counter to update automatically either - I have to problem getting it to increment when the user visits the app canvas page and something occurs, but what I want is to alert the user of a certain event without requiring the user to actually visit the app canvas.
I would guide you to the Request dialog page because it's (supposed to be) the right place to look. But as usual something is missing in the documentation.
Anyway, what you are looking for is a application-generated request:
<?php
$app_id = YOUR_APP_ID;
$app_secret = YOUR_APP_SECRET;
$token_url = "https://graph.facebook.com/oauth/access_token?" .
"client_id=" . $app_id .
"&client_secret=" . $app_secret .
"&grant_type=client_credentials";
$app_access_token = file_get_contents($token_url);
$user_id = THE_CURRENT_USER_ID;
$apprequest_url ="https://graph.facebook.com/" .
$user_id .
"/apprequests?message=’INSERT_UT8_STRING_MSG’" .
"&data='INSERT_STRING_DATA'&" .
$app_access_token . "&method=post";
$result = file_get_contents($apprequest_url);
echo("Request id number: ", $result);
?>
Described in a "blog post" instead of the documentation! and can be fount in this document (thanks #Charles).
I'm using the PHP SDK to write a simple app that looks for a certain change regarding the current user (not to seem mysterious, it's just that this part is irrelevant).
Actually, that's not entirely irrelevant, as the Facebook TOS prevents you from doing this in most situations, especially defriending, relationship statuses, etc.
As soon as this change is detected, I want to somehow alert the current user of the change, just not by posting it to the user's wall, this information needs to stay confidential - it needs to stay between the user and the app.
Request their e-mail and send it that way.
using fconnect user have authenticated and i have saved their userid to my database.
When i try to publish to their wall is redirecting to Facebook login page . If user haven't logged in.
$status = $facebook->api('/MY_USER_ID/feed', 'POST',
array('message' => 'This post came from my app.'));
Help me but some web app are posting status to my wall without even i havn't logged into facebook.
Please help me to fix it:)
below code helps you to get permissions from user and access the user's information:
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id=". $app_id . "&redirect_uri=" . urlencode($my_url) . "&scope=publish_stream,user_about_me,read_friendlists,offline_access,publish_actions,friends_photos,,user_photos". "&state=" . $_SESSION['state'];
echo("<script> window.location.href='" . $dialog_url . "'</script>");
When your application gets permissions from the user you need to specify you need the offline_access permission using the scope parameter.
You just need to ask for the publish_stream permission and then your code will work. For more about this check this and this answers.