[英]Facebook Login with Spring Social using Existing User Access Token
Here's what I currently have: 这是我现在拥有的:
My problem is that I want these APIs to be used by multiple clients, but the way Facebook Login is right now, it doesn't work well on mobile (works great on a website). 我的问题是我希望这些API被多个客户端使用,但Facebook登录的方式现在,它在移动设备上运行不佳(在网站上运行良好)。
Here's the mobile scenario: 这是移动方案:
Can this be done? 可以这样做吗? Or am I going to have to write my own API to persist the User Connection?
或者我是否必须编写自己的API以持久保存用户连接?
Appreciate any help! 感谢任何帮助!
I had the exact same issue and here's how I made it work. 我有完全相同的问题,这就是我如何使它工作。 You probably have a SocialConfigurer somewhere with the following:
您可能在某处具有SocialConfigurer,其中包含以下内容:
@Configuration
@EnableSocial
public class SocialConfig implements SocialConfigurer {
@Autowired
private DataSource dataSource;
@Bean
public FacebookConnectionFactory facebookConnectionFactory() {
FacebookConnectionFactory facebookConnectionFactory = new FacebookConnectionFactory("AppID", "AppSecret");
facebookConnectionFactory.setScope("email");
return facebookConnectionFactory;
}
@Override
public void addConnectionFactories(ConnectionFactoryConfigurer cfConfig, Environment env) {
cfConfig.addConnectionFactory(facebookConnectionFactory());
}
@Override
public UserIdSource getUserIdSource() {
return new AuthenticationNameUserIdSource();
}
@Override
public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
return new JdbcUsersConnectionRepository(dataSource, connectionFactoryLocator, Encryptors.noOpText());
}
// Other @Bean maybe ...
}
From here, what you can do is, in a Controller/RestController, add a mapping with a RequestParam for your token that you will send to your server: 从这里开始,您可以在Controller / RestController中添加一个映射,其中包含您将发送到服务器的令牌的RequestParam:
@Autowired
private FacebookConnectionFactory facebookConnectionFactory;
@Autowired
private UsersConnectionRepository usersConnectionRepository;
@RequestMapping(value = "/my-facebook-url", method = RequestMethod.POST)
public String fb(@RequestParam String token) {
AccessGrant accessGrant = new AccessGrant(token);
Connection<Facebook> connection = facebookConnectionFactory.createConnection(accessGrant);
UserProfile userProfile = connection.fetchUserProfile();
usersConnectionRepository.createConnectionRepository(userProfile.getEmail()).addConnection(connection);
// ...
return "Done";
}
Useful references 有用的参考
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.