1

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 & API secret key), as well as an access token & access token secret.

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? :,(

This is what I'm attempting (with sensitive bits replaced by dummy values):

@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;
    }

}

This is the error returned:

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)

Where did I go wrong?

4

1 回答 1

3

getScreenName 的定义

“返回验证用户的屏幕名称。此方法可能会在第一次调用时在内部调用 verifyCredentials() 如果 - 此实例由 Basic 验证并且提供电子邮件地址而不是屏幕名称,或者 - 此实例由OAuth验证。”

基于用户的身份验证必须使用 OAuth 1.0a 而不是 OAuth 2。您需要获取访问令牌,请遵循:
https ://developer.twitter.com/en/docs/basics/authentication/overview/using-oauth

当您获得访问令牌时,只需像这样更新您的 ConfigurationBuilder :

    ConfigurationBuilder cb = new ConfigurationBuilder();

    cb.setApplicationOnlyAuthEnabled(false);
    cb.setOAuthConsumerKey(CONSUMER_KEY)
    .setOAuthConsumerSecret(CONSUMER_SECRET)
    .setOAuthAccessToken(ACCESS_TOKEN)
    .setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET);
于 2018-08-16T12:45:37.943 回答