How can I follow a twitter user using Twitter4j framework? - java

I need my Java Twitter application to be able to follow a user in twitter. When I pass it the user ID as a string, the application follow it automatically. I couldn't find the method that can do that in Twitter4j.

No need for id. It will work with username as well.
mTwitter.createFriendship("dj")

Problem solved ,you can follow a user using twitter.createFriendship("twitter id"); method

The following configuration will do it:
ConfigurationBuilder cb =new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("******")
.setOAuthConsumerSecret("******")
.setOAuthAccessToken("*******")
.setOAuthAccessTokenSecret("********");
//In case of proxy
//cb.setHttpProxyHost("******").setHttpProxyPort(8080);
TwitterFactory twitterFactory=new TwitterFactory(cb.build());
Twitter twitter=twitterFactory.getInstance();
twitter.createFriendship("twitter id");

Related

RestFB getting Conversations

Iam using restfb to get in touch with Facebook from our application.
I have link to the conversation
https://www.facebook.com/prasannapatireddy/posts/234992897106899?comment_id=263969987542523
Now what I need is using above link I have to find out the Attachments if any in this conversation.
How can we do this in Java.
I tried below but didn't work
'
Connection myFeed = facebookClient
.fetchConnection("https://www.facebook.com/prasannapatireddy/posts/234992897106899?comment_id=263969987542523",Comment.class);
Please Help.
Thanks & Regards,
Prasanna
You have to provide the information in your call which fields you need. And you cannot use the perma link of a comment you see on Facebook, this is all about the Graph API.
Connection<Comment> commentConnection = facebookClient.fetchConnettion("<comment_id>",
Comment.class, Parameter.with("fields", "attachment"));
The comment id should be fetched via graph api or with some luck you can try to build it by hand.

How to make a user tweet into his/her twitter account from java web application

Hello I want to build an java web application in which I want the user to tweet on his account from my java application.
Now when we are considering twitter4J the code which is it shows is using our own registered application on twitter dev portal. Its is not asking for clients credentials. So please tell me how to figure out that I just want guidance not the code.
I read many post but all are confusing and I am not understanding.
I want to make the user tweet into his/her twitter account from java web application.
Information I gathered
http://blog.blprnt.com/blog/blprnt/quick-tutorial-twitter-processing
Read this tutorial
Import the twitter4j library – you do this by simply dragging the twitter4j-2.0.8.jar file onto your sketch window. If you want to check, you should now see this file in your sketch folder, inside of a new code directory.
We’ll put the guts of this example into the setup enclosure, but you could wrap it into a function or build a simple Class around it if you’d like:
Twitter myTwitter = new Twitter("yourTwitterUserName", "yourTwitterPassword");
But now twitter 4j is upgraded to
twitter4j-3.0.3
and now Twitter is an interface. What to do
As added by juned's answer below
I have to supply for a particular user
oauth.consumerKey=*********************
oauth.consumerSecret=******************************************
oauth.accessToken=**************************************************
oauth.accessTokenSecret=******************************************
So I know for a particular registered app twitter provides this keys. But I want to know for an random user from where to get this keys.
I believe you cannot do it by simply using username and password. You need to generate the key secret token and tokensecret. Read these properties for example from a file:
oauth.consumerKey=*********************
oauth.consumerSecret=******************************************
oauth.accessToken=**************************************************
oauth.accessTokenSecret=******************************************
And then using java code use these properties to get the access to the user account for tweeting, here is the sample code:
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("*********************")
.setOAuthConsumerSecret("******************************************")
.setOAuthAccessToken("**************************************************")
.setOAuthAccessTokenSecret("******************************************");
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();

How to get my Weibo(China SNS) tweet list using developer api in the java?

I want to get my Weibo (www.weibo.com - china sns) tweet list by api.
I am using https://code.google.com/p/weibo4j/ library.
But their sample source only contains web auth.
If i want to their accessToken, their sample source shows tutorial using web browser.
System.setProperty("weibo4j.oauth.consumerKey", Weibo.CONSUMER_KEY);
System.setProperty("weibo4j.oauth.consumerSecret", Weibo.CONSUMER_SECRET);
Weibo weibo = new Weibo();
RequestToken requestToken = weibo.getOAuthRequestToken();
...
BareBonesBrowserLaunch.openURL(requestToken.getAuthorizationURL());
I want to login weibo api using my account directly and get my timeline post list.
But I think their document does not contain this method. Is anybody already resolved its problem?
So i want to code library for using weibo api as below code.
Weibo weibo = new Weibo();
weibo.setUserAndPassword('john', 'johnpassword'); // weibo4j does not support this method.
....
Paging pag = new Paging();
pag.setSinceId(3343021761165196l);
pag.setCount(200);
List<Status> statuses = weibo.getUserTimeline("john", pag);
Is there any java library for using weibo api?
I think there aren't any library can get weibo timeline via plaint username and password!
A short history:
Weibo api has two version, namely V1 and V2.
In V1 version api, they actually support this kind of login, check this example.
However, after V2 version api was created, all app must use oauth2.

Twitter4J Access token already available

I am receiving error messages while working with Twitter4J:
java.lang.IllegalStateException: Access token already available.
twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:112)
twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:104)
twitter4j.TwitterBaseImpl.getOAuthRequestToken(TwitterBaseImpl.java:276)
twitter4j.TwitterBaseImpl.getOAuthRequestToken(TwitterBaseImpl.java:269)
[...]
This exception is thrown while calling the method Twitter.getOAuthRequestToken(). I want to get the Authorization URL to authenticate the next user.
How am I possible to solve this problem? I only put the OAuthConsumerKey, the OAuthConsumerSecret, the OAuthAccessToken and the OAuthAccessTokenSecret to the Twitter4J properties. But how do I receive the authorization URL to authenticate a new user?
Thanks and greetings,
Martin
Sorry.
I was setting an Access Token hard coded by the Configuration Builder.
Removed it, works now.
You need to create a new instance of twitter and null it's accessToken and accessTokenSecret fields before requesting new access token.
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey(TWITTER_CONSUMER_KEY)
.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET)
.setOAuthAccessToken(null)
.setOAuthAccessTokenSecret(null);
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();

How to retrieve home timeline from twitter in the form of .xml or JSON using java?

I want to get a home timeline from twitter and I was able to get the home timeline using twitter4j and oauth authentication method
ConfigurationBuilder confBuilder = new ConfigurationBuilder();
confBuilder.setOAuthAccessToken(accessToken.getToken())
.setOAuthAccessTokenSecret(accessToken.getTokenSecret())
.setOAuthConsumerKey(key)
.setOAuthConsumerSecret(secret);
Twitter twit = new TwitterFactory(confBuilder.build()).getInstance();
User user = twitter.verifyCredentials();
List<Status> statuses = twitter.getHomeTimeline();
but the result is not in the form of .xml or JSON. i also tried
WebResource resource = client.resource("https://api.twitter.com/1/statuses/user_timeline.json");
but all I get is GET https://api.twitter.com/1/statuses/user_timeline.json returned a response status of 401 Unauthorized
I googled many times but I just cant get it right. Please I need a sample java code of how to do it. Complete code that can run right away would be really helpful as I got a lot of partially coded program and just couldnt get it to work. thank you in advance
OK, so after looking at the release notes for the 2.2.x versions, it appears there is a way to get the JSON representation from Twitter4J, but it's disabled by default since it uses some extra memory.
So, you need to:
Enable the JSONStore using the jsonStoreEnabled config option
Get the JSON representation of a request using the getRawJson method
Sorry there's no code example, I haven't tried it myself.
401 Unauthorized:
Authentication credentials were missing or incorrect.
You need to authenticate before you perform the query.

Categories