为什么使用Spring Boot/Spring Social指南示例无法建立与Twitter的空连接? [英] Why do i get a null connection to twitter with Spring Boot / Spring Social guide example?
问题描述
我按照spring.io网站上的说明进行操作: http://spring.io/指南/gs/access-twitter/
I followed the instructions on the spring.io site: http://spring.io/guides/gs/accessing-twitter/
我没有连接. "findPrimaryConnection()"调用返回空值.我没有看到任何异常.我确实在.properties文件中设置了appId和appSecret.
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.
这是控制器代码:
@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>
推荐答案
您需要在Twitter的应用程序设置中设置回调URL -这应该在Spring指南中说明.
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:
回调URL:成功认证后,我们应该返回哪里? OAuth 1.0a应用程序应在请求令牌步骤中明确指定其oauth_callback URL,无论此处给出的值如何.要限制您的应用程序不使用回调,请将该字段留空.
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.
这篇关于为什么使用Spring Boot/Spring Social指南示例无法建立与Twitter的空连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!