Arpan Das
Arpan Das

Reputation: 1037

share with facebook using Spring-Social and Spring Security

I have integrated social sign-in button [Facebook] with my web application. Its working fine using spring-social and spring security.

In my login.jsp I have:

<!-- Add Facebook sign in button -->
<a href="${pageContext.request.contextPath}/auth/facebook"><button class="btn btn-facebook"><i class="icon-facebook"></i>facebook</button></a>

Now my registrion controller is: This is the place where I am getting a callback from facebook when the user tries to login for the first time from facebook and register the user in my Database.

@RequestMapping(value = "/user/register", method = RequestMethod.GET)
    public String showRegistrationForm(WebRequest request, Model model) 
    {
        LOGGER.debug("Rendering registration page.");
        @SuppressWarnings("deprecation")
        Connection<?> connection = ProviderSignInUtils.getConnection(request);
        RegistrationForm registration = createRegistrationDTO(connection);
        LOGGER.debug("Rendering registration form with information: {}", registration);
        model.addAttribute(MODEL_NAME_REGISTRATION_DTO, registration);
        return VIEW_NAME_REGISTRATION_PAGE;
    }

And also the user is getting saved in UserConnection table.

For subsequent login also I am getting the updated connection in my WebRequest

Now I want to create a shareWithFacebook operation for a user who logged in in my application using signwithfacebook button.

For this my controller is:

@RequestMapping(method = RequestMethod.GET)
public String shareWithFacebook(WebRequest request){
  Map<String, String[]> params = request.getParameterMap();
  String[] head = request.getAttributeNames(WebRequest.SCOPE_REQUEST);
  String[] head1 = request.getAttributeNames(WebRequest.SCOPE_SESSION);
  return null;
}

Now when I am running this controller in debug mode , then I can see the Connection object is present in my WebRequest object in this controller, How I can use this connection Object to make any operation, please help

Upvotes: 1

Views: 1297

Answers (1)

Arpan Das
Arpan Das

Reputation: 1037

no help from stackoverflow: but actually I got the solution , may it help someone else, thus posting the same:

add this in your social-xml config to initialize FacebookApiHelper

<bean id="facebookApiHelper" class="org.springframework.social.facebook.config.support.FacebookApiHelper">
    <constructor-arg index="0" ref="usersConnectionRepository"/>
    <constructor-arg index="1" ref="userIdSource"/>
</bean>

Then use the same in ur contoller to work with existing connection object with facebook.

@Controller
@RequestMapping("/facebook")
public class FacebookOperationController {

    private static final Logger logger = LoggerFactory.getLogger(FacebookOperationController.class);

    @Autowired
    protected FacebookApiHelper facebookApiHelper;

    @Autowired
    UserIdSource userIdSource;

    private UsersConnectionRepository usersConnectionRepository;

    @Autowired
    public FacebookOperationController(UsersConnectionRepository usersConnectionRepository)
    {
        this.usersConnectionRepository = usersConnectionRepository;
    }

    @RequestMapping(method = RequestMethod.GET)
    public String shareWithFacebook(WebRequest request,Model model){

        Facebook facebook = facebookApiHelper.getApi();
        Connection<Facebook> connection = usersConnectionRepository.createConnectionRepository(userIdSource.getUserId()).findPrimaryConnection(Facebook.class);
        return "tilesname";
    }
}

Now we have connection and facebook , enjoy will all api..

Upvotes: 1

Related Questions