3

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>
vt97john
  • 579
  • 3
  • 10
  • 25
  • Can you post the stack trace of the exception that's getting thrown? – Shaunak Jul 31 '15 at 15:38
  • There is no exception thrown, but here's the log anyway: 2015-07-31 13:54:40.499 87372 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializ ing Spring FrameworkServlet 'dispatcherServlet' 2015-07-31 13:54:40.499 87372 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Framework Servlet 'dispatcherServlet': initialization started 2015-07-31 13:54:40.510 87372 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Framework Servlet 'dispatcherServlet': initialization completed in 11 ms ******** no connection yet: null – vt97john Jul 31 '15 at 17:55

1 Answers1

3

You need to set the Callback URL in your Twitter's application settings - that should be said in Spring guides.

For instance, I have my app working with this callback url:

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

enter image description here

It is explained right below the input field:

Callback URL: Where should we return after successfully authenticating? OAuth 1.0a applications should explicitly specify their oauth_callback URL on the request token step, regardless of the value given here. To restrict your application from using callbacks, leave this field blank.

Denis C de Azevedo
  • 6,276
  • 2
  • 32
  • 49