title: How to access Twitter API resources using Twitter's Java library (twitter4j)
body: <p>I'm new to Twitters API and Twitter's twitter4j library. I've recently registered an app to be able to use Twitter's API.  Twitter has granted me consumer API keys (API key &amp; API secret key), as well as an access token &amp; access token secret.  </p>
  
  <p>The problem is, I've been trying to use twitter4j to authenticate into twitter (using the aforementioned keys), but when trying to access any of the APIs resources, I get an error saying I'm not allowed access due to a rate limit. But how can I possibly have reached a rate limit when I've never been able to query the api? :,(</p>
  
  <p>This is what I'm attempting (with sensitive bits replaced by dummy values):</p>
  
  <pre><code>@SpringBootApplication
  public class App 
  {
  
      private static final String CONSUMER_KEY = "FakeConsumerKey";
      private static final String CONSUMER_SECRET = "FakeConsumerSecret";
  
      public static void main( String[] args )
      {
          SpringApplication.run(App.class, args);
          System.out.println("Making an authentication request to"
                  + " retrieve the bearer token...");
  
  
  
          OAuth2Token token;
          token = getOAuth2Token();
  
          ConfigurationBuilder cb = new ConfigurationBuilder();
  
          cb.setApplicationOnlyAuthEnabled(true);
          cb.setOAuthConsumerKey(CONSUMER_KEY);
          cb.setOAuthConsumerSecret(CONSUMER_SECRET);
          cb.setOAuth2TokenType(token.getTokenType());
          cb.setOAuth2AccessToken(token.getAccessToken());
  
          Twitter twitter = new TwitterFactory(cb.build()).getInstance();
  
          try {
              System.out.println("My screen name: " + twitter.getScreenName());
          } catch (IllegalStateException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (TwitterException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }   
  
     }
  
  
      public static OAuth2Token getOAuth2Token()
      {
          OAuth2Token token = null;
          ConfigurationBuilder cb;
  
          cb = new ConfigurationBuilder();
          cb.setApplicationOnlyAuthEnabled(true);
          cb.setOAuthConsumerKey(CONSUMER_KEY);
          cb.setOAuthConsumerSecret(CONSUMER_SECRET);
  
          try
          {
              token = new TwitterFactory(cb.build())
                  .getInstance().getOAuth2Token();
  
              System.out.println("token: " + token.getAccessToken());
          }
          catch (Exception e)
          {
              System.out.println("Can't get OAuth2 token");
              e.printStackTrace();
              System.exit(0);
          }
  
          return token;
      }
  
  }
  </code></pre>
  
  <p>This is the error returned:</p>
  
  <pre><code>403:The request is understood, but it has been refused. An accompanying error message will explain why. This code is used when requests are being denied due to update limits (https://support.twitter.com/articles/15364-about-twitter-limits-update-api-dm-and-following).
  message - Your credentials do not allow access to this resource
  code - 220
  
  Relevant discussions can be found on the Internet at:
      http://www.google.co.jp/search?q=9a9caf7a or
      http://www.google.co.jp/search?q=bf94ba05
  TwitterException{exceptionCode=[9a9caf7a-bf94ba05], statusCode=403, message=Your credentials do not allow access to this resource, code=220, retryAfter=-1, rateLimitStatus=null, version=4.0.6}
      at twitter4j.HttpClientImpl.handleRequest(HttpClientImpl.java:164)
      at twitter4j.HttpClientBase.request(HttpClientBase.java:57)
      at twitter4j.HttpClientBase.get(HttpClientBase.java:75)
      at twitter4j.TwitterBaseImpl.fillInIDAndScreenName(TwitterBaseImpl.java:133)
      at twitter4j.TwitterBaseImpl.fillInIDAndScreenName(TwitterBaseImpl.java:128)
      at twitter4j.TwitterBaseImpl.getScreenName(TwitterBaseImpl.java:108)
      at com.vismark.social.twitter.TwitterAccountService.App.main(App.java:41)
  </code></pre>
  
  <p>Where did I go wrong?</p>