Search code examples
twitterauthorizationspring-social

spring social twitter MissingAuthorizationException


I tried to post a tweet using Spring Social as described in this tutorial: https://docs.spring.io/spring-social-twitter/docs/current/reference/htmlsingle/. However I am getting

org.springframework.social.MissingAuthorizationException: Authorization is required for the operation, but the API binding was created without authorization.

My code looks as follows

TwitterTemplate twitterTemplate = new TwitterTemplate("key", "secret");
TweetData tweetData =new TweetData("test screenshot").withMedia(new ByteArrayResource(object.getScreenshot().getBytes()));
Tweet postResult = twitterTemplate.timelineOperations().updateStatus(tweetData);

Solution

  • certain operations require a separate access token to be passed along with TwitterTemplate:

    new TwitterTemplate(consumerKey, consumerSecret, accessToken, accessTokenSecret);
    

    access tokens can be generated here: apps.twitter.com/app/"appId"/keys