I have facebook ids of some of my friends. I want to post on my newsfeed,by using facebook graph api.
How can I make only these friends(facebook ids I have) to see my post?
Did I need to create any group for this?
Yes, it is possible. In the parameter bundle, add a privacy field. see example below:
public String postToWall() {
Bundle params = new Bundle();
params.putString("message", "test message");
JSONObject privacy = new JSONObject();
privacy.put("value", "CUSTOM");
privacy.put("friends", "SOME_FRIENDS");
privacy.put("allow", "FRIEND_ID_1,FRIEND_ID_2...");
params.putString("privacy", privacy.toString());
Facebook facebook = new Facebook(YOUR_APP_ID);
AsyncFacebookRunner mAsyncFacebookRunner = new AsyncFacebookRunner(facebook);
mAsyncFacebookRunner.request("me/feed", params, "POST", new BaseRequestListener() {
...
});
A post by its definition can be seen by everybody.
Alternatively you can
Post to a pre-created group
Message a group of friends
Related
I could check in with the older Facebook SDK used this code:
Facebook fb = new Facebook("APP ID");
Bundle params = new Bundle();
params.putString("access_token", "TOKEN");
params.putString("place", "PLACE ID");
params.putString("message","SOMETHING MESSAGE");
JSONObject coordinates = new JSONObject();
coordinates.put("latitude", "LATITUDE");
coordinates.put("longitude", "LONGITUDE");
params.putString("coordinates",coordinates.toString());
params.putString("tags", "USER ID");
String response = fb.request("me/checkins", params, "POST");
But the fb = new Facebook() and the fb.request() functions are deprecated in new 3.0 Facebook SDK! What can I replace them with? How can I check in with using the 3.0 Facebook SDK?
The Graph API need "publish_checkins" extended permission in Facebook app, but this permission is deprecated also.
You should use the static methods of the Request class to create requests and execute them.
Looks like the most suitable method would be newStatusUpdateRequest that takes a GraphPlace as a parameter.
Alternatively, you can use the newPostRequest which allows you to construct a request that is much more similar to the way it was in previous SDK.
I'm using the following snippet to request the friendslist of the authorized user:
System.out.println("AT: " + facebook.getAccessToken());
String response = facebook.request("me/friends?fields=first_name,last_name,id,gender");
The response I get:
{"error":{"message":"An active access token must be used to query information about the current user.","type":"OAuthException","code":2500}}
I'm printing the accesstoken, this gives me a valid token. Also, getAccessExpires() returns a time in the future. When I request the url "me/friends" without any params, I get the expected (but with less data) friends list without errors.
How can I solve this?
String response = facebook.request("me/friends?fields=first_name,last_name,id,gender");
That is the incorrect way to use facebook.request. I'm assuming you're not using the beta Android SDK so the correct way to do this is to do the following:
Facebook facebook = new Facebook(YOUR_APP_ID);
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
Bundle params = new Bundle();
params.put("fields", "first_name, last_name, id, gender");
mAsyncRunner.request("me/friends", params, new RequestListener() {
// get response here
....
});
Hope this helps.
Now, I know that it's common question, but since "message" parameter is not valid, I wonder how to post just message to my facebook wall?
I use this:
public void postOnWall(Facebook mFacebook) {
try{
Bundle parameters = new Bundle();
parameters.putString("caption", "Caption");
parameters.putString("description", "Description");
mFacebook.dialog(this, "feed", params, new PostDialogListener());
}
catch(Exception e){}
}
I always get empty form without any title or content. I tried all params from facebook sdk docs but still no results.
UPD: when I say empty I mean this:
Why is the message parameter invalid? If you want to only post a message on your wall its should simply be just a matter of doing the following:
Bundle params = new Bundle();
params.putString("message", "test message on my wall");
mAsyncRunner.request("feed", params, "POST", new PostDialogListener(), null);
Please see below link of Stack overflow's answer for how to integrate facebook into your android application and if u have any query regarding that then tell me.
Post status on Facebook
I am using Faccebook android API (com.facebook.android) to post the messages on facebook.
Facebook mFacebook = new Facebook(Constants.APP_ID);
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(mFacebook);
Bundle parameters = new Bundle();
parameters.putString("message", "this is a test post");// the message to post to the wall
mFacebook.dialog(this, "stream.publish", parameters, new SampleDialogListener());
Everything is working fine. I'm able to login and post the message, but after login, it is displaying a intermediate dialog, "Publish story" with a text box with my post msg, "this is a test post" that is editable by the user, but i want to post the messages immediately after login without having this intermediate post screen.
Can anybody help me how to achieve this.
Thanks,
nehatha
This is how you do it:
Bundle bundle = new Bundle(3);
bundle.putString("message", "Hello!");
mFacebook.request("me/feed", bundle, "POST");
On this page facebook tells you, that you can somehow use the Facebook-Query-Language: http://developers.facebook.com/docs/reference/fql/profile
But I just dont understand what the query looks like. On Stackoverflow I just find exapmles on how to use the FQL with PHP. But I want to use it out of and Android Application. Anybody can give me URLs or Code Examples?
Using the Facebook Android SDK, an FQL call can be implemented like this:
String query = "SELECT name FROM user WHERE uid = me()";
Bundle params = new Bundle();
params.putString("method", "fql.query");
params.putString("query", query);
mAsyncFacebookRunner.request(null, params, new FQLRequestListener());
where FQLRequestListener extends RequestListener, just as for a Graph call.
See this link https://developers.facebook.com/docs/reference/fql , and you can go deeper because you can click on each element and see all the propreties of each element , ( exemple "album" here : https://developers.facebook.com/docs/reference/fql/album/ ).
This is an exemple of query to get the url of the pictures of an album by "album_id" :
String fqlQuery = "SELECT src FROM photo WHERE album_object_id="+ <your_album_id>;
This is one of the ways to call your request :
Bundle params = new Bundle();
params.putString("format", "json");
params.putString("query", fqlQuery);
params.putString("access_token", fbAccessToken);
Session session = Session.getActiveSession();
Request request = new Request(session, "/fql",
params, HttpMethod.GET,
new Request.Callback() {
public void onCompleted(
Response response) {
Log.i(TAG, "Got results: "
+ response.toString());
}
});
Request.executeBatchAsync(request);
I hope I helped, good luck! :)
This seems to work if I put it into the Browser, but I dont know how to tell the Android Facebook SDK to use this query:
https://api.facebook.com/method/fql.query?query=select+pic_small+from+profile+where+id+%3D+USERID;&access_token=ACCESSTOKEN
I haven't tried it, but for calling fql you can try
public String request(String fql) throws FileNotFoundException, MalformedURLException, IOException {
Bundle parameters = new Bundle
parameters.putString("format", "json");
parameters.putString("query", fql);
parameters.putString("access_token", mFacebook.getAccessToken());
if (mFacebook.isSessionValid()) {
params.putString("access_token", mFacebook.getAccessToken());
}
String url = (fql != null) ? "https://api.facebook.com/method/fql.query" : "https://api.facebook.com/restserver.php";
return Util.openUrl(url, "GET", params);
}
It should work.
You have to take into acount that mFacebook is an authenticated Facebook Object (of the Facebook Android SDK). Also, that the fql sould be filled with a format supported by an url. For example:
select+pic_small+from+profile+where+id+%3D+USERID;
instead of
select pic_small from profile where id=USERID;