Announcement Announcement Module
No announcement yet.
Facebook : How to "store" an accesstoken obtained via javascript in userconnection Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Facebook : How to "store" an accesstoken obtained via javascript in userconnection

    I have an app that is accessed both via web and via phonegap.

    When using the web, I am using the spring social framework (i.e doing a post on /provider/facebook).
    So the userconnection table is updated.

    In phonegap, I use a plugin to use the native facebook sdk (
    Then I send the token to the server so that I can do the auth :
    FacebookTemplate fb = new FacebookTemplate(facebookAccessToken);
    FacebookProfile p = api.userOperations().getUserProfile();
    FfUser u = userDao.loadByEmail(p.getEmail());

    The problem when I do this is that I am "bypassing" spring social.

    So later (i.e in another request) when I do :
    Facebook facebook = conRepo.createConnectionRepository(searcher.getId( )).getPrimaryConnection(Facebook.class).getApi();
    List<FbFriend> friends = facebook.fetchConnections("me", "friends", FacebookFriends.FbFriend.class, "name", "installed");

    I have a ExpiredAuthorizationException (because I am using the token obtained via the web and now only the token obtained via JS is valid).

    So my question is : how do I update the userconnection when I am not using the ProviderSignInController ?

    I've studied the code of ProviderSignInController but it goes beyond my understanding where should I plug since I've already consumed the oauth2 workflow ...

    Any help really appreciated !

  • #2

    If you want to update an existing Facebook connection in the UsersConnectionRepository, I think you'll need the access token and an expiry time for that access token. Once you have those two pieces of information, you should be able to update the connection as follows:

    		ConnectionFactory<Facebook> facebookConnectionFactory = connectionFactoryLocator.getConnectionFactory(Facebook.class);
    // Obtain the existing connection data for Facebook stored in users connection repository
    		Connection<Facebook> existingFacebookConnection = conRepo.createConnectionRepository(searcher.getId( )).getPrimaryConnection(Facebook.class);
    		ConnectionData existingConnectionData = existingFacebookConnection.createData();
    		String accessTokenObtainedByJS = "someAccessToken";
    		// Set your expire time for the access token here instead of setting to null
    		Long expireTime = null;
    		// Create an updated connection data object for Facebook
    		ConnectionData updatedConnectionData = new ConnectionData(existingConnectionData.getProviderId(), existingConnectionData.getProviderUserId(), 
    				existingConnectionData.getDisplayName(), existingConnectionData.getProfileUrl(), 
    				existingConnectionData.getImageUrl(), accessTokenObtainedByJS, existingConnectionData.getSecret(), existingConnectionData.getRefreshToken(),expireTime);
    		// Create an updated Connection object for Facebook
    		Connection<Facebook> facebookConnection = facebookConnectionFactory.createConnection(updatedConnectionData);
    		// Update the Facebook connection in the users connection repository
    Hope this helps,



    • #3
      Thanks, I'll try that ASAP even if it is a lot more complicated than I anticipated (I thought I could plug onto ProviderSignInController directly ...)


      • #4
        Here is a "simplified" version

        The most important difference is that it supports the use case where the user never connected before (i.e do not use getPrimaryConnection to build the "new" connection)

                        AccessGrant accessGrant = new AccessGrant(facebookAccessToken, null, null, 60*86400);
        		Connection<Facebook> fb = fbConnectionFactory.createConnection(accessGrant);
        		String uId = simpleConnectionSignUp.execute(fb);
        		ConnectionRepository fbConRepo = conRepository.createConnectionRepository(uId);
        		Connection<Facebook> existingFbCon = fbConRepo.findPrimaryConnection(Facebook.class);
        		if (existingFbCon != null) {
        Much of the credits goes to Michael !
        Last edited by cblin; Dec 5th, 2012, 04:00 PM.