by BehindJava

How to fetch top 10 trending hashtags from Twitter in India using twitter4j

Home » java » How to fetch top 10 trending hashtags from Twitter in India using twitter4j

In this tutorial, we are going to learn about fetching the top 10 trending hashtags from Twitter in India using twitter4j.

Firstly follow the below steps to get

‘oauthaccesstoken’ => Access token 
‘oauthaccesstokensecret’ => Access token secret
‘consumerkey’ => API key
‘consumer_secret’ => API secret
  1. Log into the Twitter Developers section.

    • If you don’t already have an account, you can log in with your normal Twitter credentials.
  2. Go to “Create an app”.
  3. Fill in the details of the application you’ll be using to connect with the API.

    • Your application name must be unique. If someone else is already using it, you won’t be able to register your application until you can think of something that isn’t being used.
  4. Click on Create your Twitter application.
  5. Details of your new app will be shown along with your consumer key and consumer secret.
  6. If you need access tokens, scroll down and click Create my access token.

    • The page will then refresh on the “Details” tab with your new access tokens. You can recreate these at any time if you need to.

By default, your apps will be granted read-only access. To change this, go to the Settings tab and change the access level required in the “Application Type” section.

Existing apps

To get the consumer and access tokens for an existing application, go to My applications (which is available from the menu in the upper-right).

Here is the dependency that needs to be addded to pom.xml

<dependency>
    <groupId>org.twitter4j</groupId>
    <artifactId>twitter4j-core</artifactId>
    <version>4.0.7</version>
</dependency>

Java code to print the top ten trending hashtags of Twitter in India.

package hashtag.HashTagTwitter.trending;

import java.io.IOException;

import twitter4j.Trend;
import twitter4j.Trends;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;

public class TwitterTrendingHashtags {
	public static void main(String[] args) throws IOException, TwitterException {
		ConfigurationBuilder cb = new ConfigurationBuilder();
		cb.setDebugEnabled(true).setOAuthConsumerKey("nlocHt3BtD9Nix8zwwnCgunyt")
				.setOAuthConsumerSecret("rderd6uCwEToGmprcZPvuf9TEBnlDuRkA6Cb2yZHYX9fAzbg4d")
				.setOAuthAccessToken("5369123251804106754-vFFLDM448h7S9xvEXEf3diuk1Cl6iD")
				.setOAuthAccessTokenSecret("78htNOuX6LF01NBpc3zqZCSOkPFmjslSHZ4I4hVcXvcyt");
		TwitterFactory tf = new TwitterFactory(cb.build());
		Twitter twitter = tf.getInstance();
		// woeid of India 23424848
        // you can find woeid's for other locations as per your requirement
		Trends trends = twitter.getPlaceTrends(23424848);
		int count = 0;
		for (Trend trend : trends.getTrends()) {
			if (count < 10) {
				System.out.println(trend.getName());
				count++;
			}
		}
	}
}