Search code examples
javatokenaccess-tokentwitter4j

Generate new OAuthAccessToken and OAuthAccessTokenSecret in code


Can I generate new OAuthAccessToken and OAuthAccessTokenSecret in code? May be using twitter4j? I have:

configurationBuilder.setDaemonEnabled(true)
    .setOAuthConsumerKey("MyCoonsumerKey")
    .setOAuthConsumerSecret("MyConsumerSecret")
    .setOAuthAccessToken("MyAccessToken")
    .setOAuthAccessTokenSecret("MyAccessTokenSecret");

But I want generate new Tokens in process in code. Can I do it? How?


Solution

  • The simple answer to that is:

    No, you can't generate OAuthAccessToken or OAuthAccessTokenSecret by yourself via Twitter4J.

    Taken from https://dev.twitter.com/oauth/overview, we learn:

    In order to make authorized calls to Twitter’s APIs, an application must first obtain an OAuth access token on behalf of a Twitter user (or, issue Application-only authenticated requests, when user context is not required).

    Moreover, ...

    ... an access token will be invalidated if a user explicitly revokes an application in the their Twitter account settings, or if Twitter suspends an application.

    Conclusion:

    You have to generate access token & secret in "their Twitter account settings". For reference, please find the full guide under: https://dev.twitter.com/oauth/overview/introduction

    Note well, a mobile phone number is required.