I am using facebook4j-core-2.1.0 and trying to get the page likes. I don't know what I am doing wrong, I am kinda newbie at this.
public static void main(String[] args) throws FacebookException
{
// Generate facebook instance.
Facebook facebook = new FacebookFactory().getInstance();
// access token ...
AccessToken at = new AccessToken(accessTokenString);
// Set access token.
facebook.setOAuthAccessToken(at);
String facebookUserName = "Google";
Page pgId = facebook.getPage(facebookUserName);
Page pgL = facebook.getLikedPage(pgId.getId());
System.out.println("Page Likes :" + pgL);
}
http://www.facebook.com/Google shows that it have 18,281,664 likes, but when I ran the above code I got null value.
What am I missing here?
I think you should try something like this:
String userName = "google";
Page pgId = facebook.getPage(userName);
System.out.println("Page Likes :" + pgId.getLikes());
Hope it helps.
The page.getLikes() method return null to me so I opted for this:
RawAPIResponse facebookJson = facebook.callGetAPI("google?fields=engagement");
JSONObject jsonObject = facebookJson.asJSONObject();
String likesCount = ((JSONObject)jsonObject.get("engagement")).getString("count");
System.out.println("likes :" + likesCount);
Using Reading and api v2.6 see how to specify api version here http://facebook4j.org/en/faq.html#apiversion
Reading pageReading = new Reading();
pageReading.fields("name","id","fan_count");
Page page = facebook.getPage("google", pageReading);
System.out.println("likes :" + page.getFanCount());
Related
I'm trying to do a simple login to a website and at the end I print out the title to check if it's logged in, however for some reason I keep getting the title of the login screen.
Response res = Jsoup
.connect("http://moj.tvz.hr")
.data("login", "gost", "passwd", "gost")
.method(Method.POST)
.execute();
Map<String, String> cookies = res.cookies();
Document subjectPage = Jsoup.connect("https://moj.tvz.hr")
.cookies(cookies)
.get();
String subjectTitle = subjectPage.title();
System.out.println("##### Printing webpage title #####\n" + subjectTitle + "\n");
Testing login on the actual website works just fine with the user/pw combination, so I assume something is wrong with the rest of the code, but I can't seem to find what.
If you examine what data are send with a form request, for example with browser debugging tool you will find out, that for this site there is additional parameter TVZ. It is generated for your initial request. You have to parse it out and then add to login form request.
When you are connecting to other pages you have to add TVZ as a parameter to your request. Also you have to use cookies from initial request, because login response does not return any.
See code below.
Response initResponse = Jsoup.connect("http://moj.tvz.hr").execute();
Document doc = initResponse.parse();
String tvz = doc.select("input[name=TVZ]").attr("value");
Map<String, String> cookies = initResponse.cookies();
Response res = Jsoup.connect("https://moj.tvz.hr").data("login", "gost", "passwd", "gost")//
.data("TVZ", tvz)//
.cookies(cookies)//
.method(Method.POST).execute();
System.out.println("##### Printing webpage title #####\n" + res.parse().title() + "\n");
Document subjectPage = Jsoup.connect("https://moj.tvz.hr").data("TVZ", tvz).cookies(cookies).get();
String subjectTitle = subjectPage.title();
System.out.println("##### Printing webpage title #####\n" + subjectTitle + "\n");
I want to get tweets from certain user timelines using java library twitter4j, currently I have source code which can get ~ 3200 tweets from user time line but I can't get full tweet. I have searched in various sources on the internet but I can't find a solution to my problem. anyone can help me or can anyone provide an alternative to get a full tweet from the user timeline with java programming?
my source code :
public static void main(String[] args) throws SQLException {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("aaa")
.setOAuthConsumerSecret("aaa")
.setOAuthAccessToken("aaa")
.setOAuthAccessTokenSecret("aaa");
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
int pageno = 1;
String user = "indtravel";
List statuses = new ArrayList();
while (true) {
try {
int size = statuses.size();
Paging page = new Paging(pageno++, 100);
statuses.addAll(twitter.getUserTimeline(user, page));
System.out.println("***********************************************");
System.out.println("Gathered " + twitter.getUserTimeline(user, page).size() + " tweets");
//get status dan user
for (Status status: twitter.getUserTimeline(user, page)) {
//System.out.println("*********Place Tweets :**************\npalce country :"+status.getPlace().getCountry()+"\nplace full name :"+status.getPlace().getFullName()+"\nplace name :"+status.getPlace().getName()+"\nplace id :"+status.getPlace().getId()+"\nplace tipe :"+status.getPlace().getPlaceType()+"\nplace addres :"+status.getPlace().getStreetAddress());
System.out.println("["+(no++)+".] "+"Status id : "+status.getId());
System.out.println("id user : "+status.getUser().getId());
System.out.println("Length status : "+status.getText().length());
System.out.println("#" + status.getUser().getScreenName() +" . "+status.getCreatedAt()+ " : "+status.getUser().getName()+"--------"+status.getText());
System.out.println("url :"+status.getUser().getURL());
System.out.println("Lang :"+status.getLang());
}
if (statuses.size() == size)
break;
}catch(TwitterException e) {
e.printStackTrace();
}
}
System.out.println("Total: "+statuses.size());
}
Update :
After the answer given by #AndyPiper
the my problem is every tweet that I get will be truncated or not complete. a tweets that I get will be truncated if the length of tweet more than 140 characters. I found the reference tweet_mode=extended, but I do not know how to use it. if you know something please tell me.
Your Configuration should be like this:
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("aaa")
.setOAuthConsumerSecret("aaa")
.setOAuthAccessToken("aaa")
.setOAuthAccessTokenSecret("aaa")
.setTweetModeExtended(true);
It is explained well here
The Twitter API limits the timeline history to 3200 Tweets. To get more than that you would need to use the (commercial) premium or enterprise APIs to search for Tweets by a specific user.
if you are streaming tweets
fist: you have to add .setTweetModeExtended(true); into your configurationbuilder
second(here is the code)
TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
StatusListener listener = new StatusListener(){
public void onStatus(Status status) {
System.out.println("-------------------------------");
if(status.isRetweet()){
System.out.println(status.getRetweetedStatus().getText());
}
else{
System.out.println(status.getText());
}`
its totally works for me.
take care yourself :)
If you are implementing twitter api using twitter4j.properties file and getting truncated timeline text value,simply add the below property in it at the end.
tweetModeExtended=TRUE
I am trying to post a message onto a facebook group (I am the admin for the page). Here is the java code that I am using:
public void makeTestPost() {
fbClient = new DefaultFacebookClient(groupPageAccessToken);
counter = 0;
fbClient.publish(groupID + "/posts", FacebookType.class, Parameter.with("message", Integer.toString(counter) + ": Hello, fb World!"));
counter++;
}
with:
private final string groupPageAccessToken = "XXXXXXXXXXXXXXXXXX";
private final String groupID = "XXXXXXX";
I got these values using the facebook graph explorer api online Graph API Explorer
But when I login to facebook I don't see any message/post on the group. Please tell me how to make it work?
You need an user access token with the permissions publish_actions and user_managed_groups.
Then you can publish a new message like this:
GraphResponse response = fbClient.publish(groupID + "/feed", GraphResponse.class, Parameter.with("message", Integer.toString(counter) + ": Hello, fb World!"));
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"));
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.