编程语言
首页 > 编程语言> > java-为什么我在Spring Boot / Spring Social指南示例中无法建立与Twitter的空连接?

java-为什么我在Spring Boot / Spring Social指南示例中无法建立与Twitter的空连接?

作者:互联网

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

而且我没有连接. “ findPrimaryConnection()”调用返回空值.我没有看到任何异常.我确实在.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>

解决方法:

您需要在Twitter的应用程序设置中设置回调URL-在Spring指南中应该说明.

例如,我让我的应用使用此回调网址:

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.

标签:spring-social,spring-boot,spring-social-twitter,spring,java
来源: https://codeday.me/bug/20191119/2040127.html