access facebook page information - android

I want to get facebook page about, description, location, basic info etc. I have got the access token for page. I am getting likes of page like this:
public void getLikes (String id,final TextView likes,final int position)
{
Session session = Session.getActiveSession();
Bundle params = new Bundle();
params.putString("id", id);
params.putString("fields", "likes");
Request request = new Request(session, "search", params, HttpMethod.GET, new Request.Callback() {
#Override
public void onCompleted(Response response) {
try {
JSONObject res = response.getGraphObject().getInnerJSONObject().getJSONArray("data").getJSONObject(0);
if (res.length()>1){
likes.setText(String.valueOf(res.getInt("likes")));
Preferences.data.getData().get(position).setLikes(String.valueOf(res.getInt("likes")));
}
else {
Preferences.data.getData().get(position).setLikes("No Likes");
likes.setText("0");
}
} catch (Exception e) {
}
}
});
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
}
If I add more parameters in bundle, I get the value of last parameter added, what should I do. Facebook's documentation about graph api is not much clear.

This isn't an issue specific to the API, you're just only using one field of the response (.getJSONObject(0))
if you request multiple fields you need to look at each of them, currently you're only fetching the first

Related

How to set different "limit" parameters for feed and likes in Android Facebook SDK?

I am trying to implement a method in Android by using Facebook SDK to make a GraphRequest to have the feed of the currently logged in user.
public void getMyStatusInformation(AccessToken accessToken){
GraphRequest graphRequest = new GraphRequest(
accessToken,
"/me/feed",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
JSONObject responseObject = response.getJSONObject();
try {
onStatusesReceived(response);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
);
Bundle parameters = new Bundle();
parameters.putString("fields", "likes,story");
parameters.putString("limit", "2");
graphRequest.setParameters(parameters);
graphRequest.executeAsync();
}
I want to get only last 2 posts of the user, but get all the likes of his/her friends to those posts. However, when I execute this GraphRequest, the response contains 2 posts with maximum number of 2 likes.
I tried /me/feed?limit=2, had address not found error.
Tried
parameters.putString("fields", "likes,story.limit(2)"); had the error "Story parameter does not support subfield limit"
Changed the order as:
parameters.putString("limit", "2");
parameters.putString("fields", "likes,story");
But nothing changed.
How can I do this?

Android: Sharing images via Facebook API

I am trying to share an image via the android facebook API to facebook.
Sharing text and links works fine, uploading an image, too.
I found out, that I only can post images, if they are online or in the photoalbum of the user.
Bundle bundle = new Bundle();
bundle.putParcelable("picture", b);
Request request = new Request(session, "me/photos", bundle,
HttpMethod.POST, new Request.Callback() {
#Override
public void onCompleted(Response response) {
String imageid = (String) response.getGraphObject()
.getProperty("id");
share(session, text, imageid);
}
});
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
imageid is a number like "123456789908765345678"
After the execution, "share" is called:
Bundle bundle = new Bundle();
bundle.putString("caption", "some text");
bundle.putString("description", "Imageid:"+imageid);
bundle.putString("link",
"https://link.de");
bundle.putString("name", "some text");
bundle.putString("place", imageid); //It doesn't work for me
new WebDialog.FeedDialogBuilder(this, session, bundle).build().show();
I don't know what I am doing wrong. I simply want so share this image.
Help please :)
You have to make another request with the id of the resource in order to get the url of the image you are trying to reference in the share dialog.
So you should do something like this:
Callback callback = new Callback() {
#Override
public void onCompleted(Response response) {
if (response.getGraphObject() != null) {
String imageUrl = (String) response.getGraphObject().getProperty("picture");
// call to your sharing method
share(session, text, imageUrl);
}
}
};
Request request = new Request(Session.getActiveSession(), imageid, null, HttpMethod.GET, callback);
request.executeAsync();
You can upload the photo to facebook album using
com.facebook.Request fbRequest = com.facebook.Request.newUploadPhotoRequest(session, image, callback);
fbRequest.executeAsync();

Android Facebook SDK - Get user events

i've being able to login and get the user infos from the api with this code:
Facebook facebook = new Facebook("XX");
AccessToken token = AccessToken.createFromExistingAccessToken("XX", new Date(facebook.getAccessExpires()),
new Date(facebook.getLastAccessUpdate()),
AccessTokenSource.FACEBOOK_APPLICATION_NATIVE, Arrays.asList(permissions));
Session.openActiveSessionWithAccessToken(getActivity(), token, callback);
session = Session.getActiveSession();
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
#Override
public void onCompleted(GraphUser user, Response response) {
Object proper = user.getFirstName();
System.out.println(proper);
}
});
I'm using this fixed tokens cause its always come from the same user as some sort of a host but i need to get the user events and havent seen any documentation that shows how to do it, does anyone have any idea of how to get the user events that he created?
thank you!
I've managed to achieve what i want using a FQL Query with this code:
String fqlQuery = "SELECT eid, name, pic, creator, start_time FROM event WHERE eid IN (SELECT eid FROM event_member WHERE uid='XX' and rsvp_status='attending')";
Bundle params = new Bundle();
params.putString("q", fqlQuery);
Request request = new Request(session, "/fql", params, HttpMethod.GET,
new Request.Callback() {
#Override
public void onCompleted(Response response) {
System.out.println("Result: " + response.toString());
}
});
Request.executeBatchAsync(request);

Facebook sdk ignores fields filtering

I'm using the newest Facebook SDK for Android and try to get the albums of a user but only several fields of them.
My problem is that the fields param gets ignored and I get all data like likes etc....
What's wrong with my request?
Bundle params = new Bundle();
params.putString("fields", "count,name,id,created_time,updated_time,photos");
Request request = new Request(session, "me/albums", params, HttpMethod.GET, new Request.Callback() {
#Override
public void onCompleted(Response response) {
// TODO Auto-generated method stub
System.out.println(response);
}
});
Requires the user_photos or friend_photos permission
You need that permission!

Get friend interests using android facebook SDK

In my app my intention is to get some interests of a friend like music, books, tv. This information is public. For this I think I don't need to send a permission.
Based on facebook (poor)documentation, mainly this links:
Field Expansion to get friend interest I only need to pass friend ID in the expanded friends field.
Doing some tests on GraphExplorer I see it creates the following query to get movies form some friend:
MY_ID?fields=friends.uid(FRIEND_ID).fields(movies)
I use this and execute execute this code on a Request.newMyFriendRequest asynchronously:
Session activeSession = Session.getActiveSession();
if(activeSession.getState().isOpened()){
Request request = Request.newMyFriendsRequest(activeSession,
new GraphUserListCallback(){
#Override
public void onCompleted(List<GraphUser> users, Response response){
GraphUser user = users.get(0);
JSONObject friendLikes = user.getInnerJSONObject();
try {
JSONArray data = friendLikes.getJSONObject("friends").getJSONArray("data");
Log.i("JSON", friendLikes.toString());
addInterests(data, 0);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle bundle = new Bundle();
bundle.putString("fields", "friends.uid("+friendID+").fields(movies)");
request.setParameters(bundle);
request.executeAsync();
All times I execute this code I receive the following error from JSON:
W/System.err(694): org.json.JSONException: No value for friends.
If this isn't the way to get friend interests using GraphAPI, what do I need to do to get these values?
[EDIT] Solved posting friends_likes permission on Login. Permission is not sended inside Request.
I modified a bit my solution. If anyone is interested:
String fqlQuery = "SELECT music, books, movies FROM user where uid ="+friendID;
Bundle bundle = new Bundle();
bundle.putString("q", fqlQuery);
Request request = new Request(activeSession, "/fql", bundle, HttpMethod.GET,
new Request.Callback() {
#Override
public void onCompleted(Response response) {
// TODO Auto-generated method stub
Log.i("INFO", response.toString());
}
});
Request.executeBatchAsync(request);

Categories

Resources