Search code examples
javaurltwitterlong-integertwitter4j

getting userID from a given twitter url page - Twitter4j


How do I get the user id number(long) from his url twitter page?

Long userId;
URL url = new URL("https://twitter.com/MarketWatch");

Solution

  • First you have to get the User of given screen name. Extract screen name from URL by using string methods, and use showUser(java.lang.String screenName). Then you'll be able to get the id of specified user:

    Twitter twitter = TwitterFactory.getSingleton();
    // Initialize your Twitter
    User user = twitter.showUser("MarketWatch");
    long userId = user.getId(); // Long ID of user @MarketWatch
    

    Read about GET users/show in Twitter API:

    Since retrieving user data requires authentication, you have to provide your access token as well. Read about how to initialize your Twitter authenticating with OAuth: