java - 为什么我使用 Spring Boot/Spring Social 指南示例获得到 Twitter 的空连接?

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

我按照 spring.io 网站上的说明进行操作:http://spring.io/guides/gs/accessing-twitter/

我没有连接上。 “findPrimaryConnection()”调用返回空值。我没有看到抛出任何异常。我确实在 .properties 文件中设置了 appId 和 appSecret。

Controller 代码如下:

@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 指南中说明。

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

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

enter image description here

它在输入字段的正下方解释:

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.

关于java - 为什么我使用 Spring Boot/Spring Social 指南示例获得到 Twitter 的空连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31682351/

相关文章:

java - 如何从解析中提取对象并将其显示在列表中,而无需在 android 中使用它们的 ID?

java - 优雅地关闭具有静态成员身份的 Kafka Consumer

java - Maven、spring、tomcat7 : What do I need in pom. xml 部署正确?

elasticsearch - 如何在 spring boot 应用程序中显示 elasticsearch 查询

java - Spring JUnit 测试未加载完整的应用程序上下文

java - 如何在 notifyDataSetChanged 后检索 GridView 对象?

java - 如何将SIGINT传递给用java创建的进程?

java - SpringMVC 配置 applicationContext.xml 应该导入 some-servlet.xml

spring - GeoPoint 与 Spring Data ElasticSearch 给出错误 : QueryParsingException[field [location] is not a geo_point field]

java - Jetty 升级到 9.4.19.v20190610 后,传入的客户端请求中没有 X509 证书