Why do i get a null connection to twitter with Spring Boot / Spring Social guide example?

时间:2015-07-28 16:39:53

标签: java spring spring-boot spring-social spring-social-twitter

I followed the instructions on the spring.io site: http://spring.io/guides/gs/accessing-twitter/

And I'm not getting a connection. The "findPrimaryConnection()" call is returning null. I'm not seeing any exceptions thrown. I did set the appId and appSecret in the .properties file.

Here is the controller code:

@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) {
            System.out.println("******** no connection yet: " + connectionRepository.findPrimaryConnection(Twitter.class));
            return "redirect:/connect/twitter";
        }
        System.out.println("******** connection found");
        model.addAttribute(twitter.userOperations().getUserProfile());
        CursoredList<TwitterProfile> friends = twitter.friendOperations().getFriends();
        model.addAttribute("friends", friends);

        return "hello";
    }

}

Here is the HTML form:

<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>

1 个答案:

答案 0 :(得分:3)

您需要在Twitter的应用程序设置中设置回拨网址 - 应该在Spring指南中说明。

例如,我的应用程序使用此回调网址:

http://127.0.0.1:3000/auth/twitter/callback

enter image description here

输入字段正下方说明:

  

回调网址:成功验证后我们应该返回哪里?无论此处给出的值如何,OAuth 1.0a应用程序都应在请求令牌步骤中明确指定其oauth_callback URL。要限制您的应用程序使用回调,请将此字段留空。