Recently I've been struggling with getting user information from post_id with java. I'm new in restfb, but after reasearching, below code should work. All available permissions are granted. Even in Graph API Explorer when writing post_id I cannot retrieve post's author details.
This is how I do it:
FacebookClient facebookClient = new DefaultFacebookClient(token);
String command = "orangepolska/feed";
Connection<Post> pagePosts = facebookClient.fetchConnection(command, Post.class);
ArrayList<String> postList = new ArrayList<String>();
String row;
for( List<Post> posts : pagePosts){
for (Post post : posts) {
if (post.getCreatedTime().after(startDate) && post.getCreatedTime().before(endDate)){
String message = post.getMessage();
CategorizedFacebookType postedBy = post.getFrom();
Post.Comments comments = post.getComments();
row = " owner: "+postedBy.getName()+" owner_id: "+postedBy.getId()+" post: "+message+" + " likes: "+post.getLikesCount() + "\n";
System.out.println(row);
postList.add(row);
}
}
}
return postList;
The problem occurs with various of functions like: getName(), getID(), getLikesCount() etc - these return null.
How can i fix it?
Thanks in advance.
You need to fetch the feed with the fields parameter so Facebook knows which fields you need to be filled. RestFB can only provide access to information that are given by Facebook ;)
Have a look here: http://restfb.com/#selecting-specific-fields
Norbert is correct but the link he posted did not work for me - you need to include "Parameter.with("fields", "from")" to get the user information.
Connection<Post> pagePosts = facebookClient.fetchConnection(command, Post.class, Parameter.with("fields", "from"));
Related
I am looking to write code that takes a video ID as input and retrieves the comments made on the corresponding video. Here's a link to the API docs. I tried this code
String videoId = "id";
YouTube.Comments.List list2 = youtube.comments().list(Arrays.asList("snippet"));
list2.setId(Arrays.asList(videoId));
list2.setKey(apiKey);
Comment c = list2.execute().getItems().get(0);
but I get an IndexOutOfBoundsException on the last line because getItems is returning an empty List. I set videoId as a valid YouTube video ID (one which I have already successfully been able to get video data like views, title, etc from), thinking that would work but clearly I was wrong. Unless I missed something I can't find anything in the docs for the Video class about getting comment data, so that's why I'm turning to SO for help again.
EDIT: Per stvar's comment I tried changing the second line of the above code to
YouTube.CommentThreads.List list2 = youtube.commentThreads().list(Arrays.asList("snippet"));
and of course changed the type of c to CommentThread.
It is the CommentThreads API I'm supposed to use, right? Either way, this is also returning an empty list...
Here is the complete Java code that retrieves all comments (top-level and replies) of any given video:
List<Comment> get_comment_replies(
YouTube youtube, String apiKey, String commentId)
{
YouTube.Comments.List request = youtube.comments()
.list(Arrays.asList("id", "snippet"))
.setParentId(commentId)
.setMaxResults(100)
.setKey(apiKey);
List<Comment> replies = new ArrayList<Comment>();
String pageToken = "";
do {
CommentListResponse response = request
.setPageToken(pageToken)
.execute();
replies.addAll(response.getItems());
pageToken = response.getNextPageToken();
} while (pageToken != null);
return replies;
}
List<CommentThread> get_video_comments(
YouTube youtube, String apiKey, String videoId)
{
YouTube.CommentThreads.List request = youtube.commentThreads()
.list(Arrays.asList("id", "snippet", "replies"))
.setVideoId(videoId)
.setMaxResults(100)
.setKey(apiKey);
List<CommentThread> comments = new ArrayList<CommentThread>();
String pageToken = "";
do {
CommentThreadListResponse response = request
.setPageToken(pageToken)
.execute();
for (CommentThread comment : respose.getItems()) {
CommentThreadReplies replies = comment.getReplies();
if (replies != null &&
replies.getComments().size() !=
comment.getSnippet().getTotalReplyCount())
replies.setComments(get_comment_replies(
youtube, apiKey, comment.getId()));
}
comments.addAll(response.getItems());
pageToken = response.getNextPageToken();
} while (pageToken != null);
return comments;
}
You'll have to invoke get_video_comments, passing to it the ID of the video of your interest. The returned list contains all top-level comments of that video; each top-level comment has its replies property containing all the associated comment replies.
I want to extract just user Posts using RestFB but it seems there is no way to do it.
/{page-id}/feed - gives all posts Page owner + users
/{page-id}/posts - gives all the owner posts
But i want just user posts, any way ?
Connection<Post> postsPages = facebookClient.fetchConnection(page + "/feed",Post.class,
Parameter.with("fields", "id,name,message,story,link,description,created_time,likes.limit(10).summary(true),"
+ "comments.limit(10).summary(true),shares"),
// add comments (comments.limit(10000).summary(true){id,message}
Parameter.with("until", "now"),
Parameter.with("since", START_DATE),
Parameter.with("limit", 100));
List<Post> posts = new ArrayList<Post>();
I'm here again to ask for your help.
I'm building a simple application that gets all the comments from a page that I liked. It works very well, but during some tests I got HTTP Status 500 as exception.
The problem occours when the URI of the pase has the name of the page. Let me explain with two examples:
WORKING URI: https://www.facebook.com/pages/ULTRAS-NAPOLI/101174668938?fref=ts
NOT WORKING URI: https://www.facebook.com/FASTWEB?fref=ts
As you can see the difference between the two URIs is that 1. has page's ID, and 2. has page's name!
Here's my code:
public LinkedList<String> writeOnePageComments(String id){
LinkedList<String> s = new LinkedList<String>();
Connection<Post> page = fb.fetchConnection(id.trim()+"/posts", Post.class,Parameter.with("limit",999));
List<Post> pagePosts = page.getData();
List<Comment> commentList;
Comments comments;
for(int i=0;i<pagePosts.size();i++){
Post p=pagePosts.get(i);
comments=p.getComments();
if(comments!=null){
commentList = comments.getData();
if(!commentList.isEmpty())
for(int k=0;k<commentList.size();k++){
String tmp = commentList.get(k).getMessage();
tmp = f.subEmoticons(tmp);
tmp = f.removeRepeatedVocals(tmp);
s.add(tmp);
}
}
}
return s;
Any help? I'm going CRAZY! :)
SOLVED:
Connection<Post> page = fb.fetchConnection(id.trim()+"/posts", Post.class,Parameter.with("limit",999));
Should be changed to:
Connection<Post> page = fb.fetchConnection(id.trim()+"/feed", Post.class,Parameter.with("limit",999));
According to Facebook Graph API Documentation, "feed" means: "The feed of posts (including status updates) and links published by this page, or by others on this page. " , while "/posts" shows only the posts that were published by this page.
Hello i want to display a user timeline in a list with twitter4j, because im new to java i just don't know how to do that. I searched the web but couldn't find anything usefull. So my question is: does someone know how to display a user timeline in a list in android?
twitter4j-core-2.2.6
Twitter tw = new TwitterFactory().getInstance();
// some userlist
UserList userList = tw.getUserLists("screen name", -1).get(0);
System.out.println(userList.getName());
int listId = userList.getId();
ResponseList<Status> userListStatuses = tw.getUserListStatuses(listId, new Paging(1));
for (Status status : userListStatuses) {
System.out.println(status.getText());
}
As per the twitter4j Code Examples
Twitter twitter = new TwitterFactory().getInstance();
List<Status> statuses = twitter.getFriendsTimeline();
System.out.println("Showing friends timeline.");
for (Status status : statuses) {
System.out.println(status.getUser().getName() + ":" +
status.getText());
}
Please check official document and code example for twitter4j
I am trying to get the lastname of my profile using restfb.But but each time the username is returned as null.I already have the acess token and permissions.I guess t some problem with the JSon object passing.how can i pass the json objects to a javabean and later retrieve it?
This happens when the access token you provided doesn't have the correct permissions to access the data. Best way to check this is by using the facebook graph API interface; noting the version.
https://developers.facebook.com/tools/explorer/145634995501895/?method=GET&path=me&version=v2.4
FacebookClient fbClient = new DefaultFacebookClient(accessToken, Version.VERSION_2_4);
User me = fbClient.fetchObject("me", User.class, Parameter.with("fields", "email,first_name,last_name,gender"));
Note: Your FB.login function must contain the correct scope for fields you want to access.
FB.login(function(response) {
...
}, {scope: 'email'});
This works for me:
FacebookClient facebookClient = new DefaultFacebookClient(MY_ACCESS_TOKEN);
User user = facebookClient.fetchObject("me", User.class);
out.println("Last name: " + user.getLastName());
Here is the code snippet
FacebookClient facebookClient = new DefaultFacebookClient("register a facebook application with required permissions, get that application token and paste here");
User user = facebookClient.fetchObject("me", User.class);
Connection<User> myFriends = facebookClient.fetchConnection("me/friends", User.class);
for(User friend:myFriends.getData())
{
Connection<Page> myMovies = facebookClient.fetchConnection(friend.getId() + "/movies", Page.class);
content = content + "Name: " + friend.getName();
for(Page page:myMovies.getData())
{
content = content + "\n" + "Movies: " + page.getName() + "\n";
}
}
In this example your application needs "friends_likes" permission.
Hope this helps.