问题
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:
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
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.
来源:https://stackoverflow.com/questions/31682351/why-do-i-get-a-null-connection-to-twitter-with-spring-boot-spring-social-guide