Keep getting 401 (authorization required) with spring social twitter

I tried this spring social access to Twitter data guide . And although I double-checked, triple and so, checked everything that I continue to receive this error when I click the "Connect to Twitter" button:

POST request for "https://api.twitter.com/oauth/request_token" resulted in 401 (Authorization Required); invoking error handler

Here is my code:

src/main/resources/templates/connect/twitterConnect.html

 <html> <head> <title>Hello Twitter</title> </head> <body> <h3>Connect to Twitter</h3> <form action="/connect/twitter" method="POST"> <div class="formInfo"> <p>You aren't connected to Twitter yet. Click the button to connect this application with your Twitter account.</p> </div> <p><button type="submit">Connect to Twitter</button></p> </form> </body> 

src/main/resources/templates/connect/twitterConnected.html

 <html> <head> <title>Hello Twitter</title> </head> <body> <h3>Connected to Twitter</h3> <p> You are now connected to your Twitter account. Click <a href="/">here</a> to see your Twitter friends. </p> </body> 

src/main/resources/templates/hello.html

 <html> <head> <title>Hello Twitter</title> </head> <body> <h3>Hello, <span th:text="${twitterProfile.name}">Some User</span>!</h3> <h4>These are your friends:</h4> <ul> <li th:each="friend:${friends}" th:text="${friend.name}">Friend</li> </ul> </body> 

src/main/java/hello/HelloController.java

 @Controller @RequestMapping("/") public class HelloController { private Twitter twitter; private ConnectionRepository connectionRepository; @Inject public HelloController(Twitter twitter, ConnectionRepository connectionRepository) { this.twitter = twitter; this.connectionRepository = connectionRepository; } @RequestMapping(method=RequestMethod.GET) public String helloTwitter(Model model) { if (connectionRepository.findPrimaryConnection(Twitter.class) == null) { return "redirect:/connect/twitter"; } model.addAttribute(twitter.userOperations().getUserProfile()); CursoredList<TwitterProfile> friends = twitter.friendOperations().getFriends(); model.addAttribute("friends", friends); return "hello"; } } 

src/main/java/hello/Application.java

  @Configuration @EnableAutoConfiguration @ComponentScan public class Application { /* * SPRING BOOTSTRAP MAIN */ public static void main(String[] args) { SpringApplication.run(Application.class, args); } } 
+5
source share
2 answers

I ran into the same problem.

After some investigation, it turned out that the problem was with the callback URL. Spring social sets this to yourap / signin / twitter. For facebook and linkedin this is good, but for some reason, twitter does not populate the callback url in the app settings.

So, the solution: go to your Twitter app settings and fill in the callback URL (it doesn't even have to be the actual URL of the callback you are going to use, just fill in any URL!). This is most likely the cause of your problems.

+16
source

What does your application.properties file look like? It should have entries for spring.social.twitter.appId and spring.social.twitter.appSecret, filled with the values ​​that you get when you register your application on Twitter.

Make sure that you have application.properties parameters and that there are no curly brackets around the values ​​(curly brackets are displayed in the text of the manual, but they are intended as placeholders, not what you really should have in application.properties).

+1
source

Source: https://habr.com/ru/post/1209859/


All Articles