Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep getting 401 (Authorization Required) with spring social twitter

I've been trying this Spring Social Accessing Twitter Data guide. And though I've double, triple an so on checked everything I keep getting this error when I click "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);
    }

    }
like image 363
Skeeve Avatar asked Dec 24 '14 22:12

Skeeve


Video Answer


2 Answers

I ran into the same problem.

After some investigation I found out the problem is in the callback url. Spring social sets this to yourap/signin/twitter. For facebook and linkedin this is fine, but for some reason twitter nees a filled in callback url in the application settings as well.

So the solution: Go to your twitter application settings, and fill in a callback url (this doesn't even have to be the actual callback url your going to use, just fill in any url!). Most likely this is the cause of your problems.

like image 76
Sander Agricola Avatar answered Nov 02 '22 08:11

Sander Agricola


What does your application.properties file look like? It should have entries for spring.social.twitter.appId and spring.social.twitter.appSecret, populated with values you get when registering your application with Twitter.

Be sure that you have those in application.properties and that there are no curly-braces around the values (the guide's text shows curly-braces, but those are meant as placeholders, not something you should actually have in application.properties).

like image 39
Craig Walls Avatar answered Nov 02 '22 07:11

Craig Walls