我按照 spring.io 网站上的说明进行操作:http: //spring.io/guides/gs/accessing-twitter/
而且我没有连接。“findPrimaryConnection()”调用返回 null。我没有看到任何异常抛出。我确实在 .properties 文件中设置了 appId 和 appSecret。
这是控制器代码:
@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";
}
}
这是 HTML 表单:
<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>