Analyzing Spring Social Facebook Data with Watson Personality Insights

4 min read

Analyzing Spring Social Facebook Data with Watson Personality Insights

In this post, I’ll show you how to build a basic Spring app with Facebook login using Spring Social. Then we’ll use Watson Personality Insights to analyze the profile of the logged-in user. The project we will create will be similar to the Accessing Facebook Data Spring guide, but with a few modifications.

Prerequisites

Before you begin, you’ll need to have Java and Maven installed and configured on your PATH.

Registering the Application with Facebook

The first step is to register a new application with Facebook. Go the the Facebook Developer site and login or register as a Facebook developer. This exact signup process is subject to change, but should be fairly intuitive.

After you are signed in to your Facebook developer account, click My Apps in the top-right corner. This should bring you to a page displaying all of your apps. Create a new app by clicking the Add a New Appbutton. Complete the form for your new app, naming it whatever you choose. I will call my app “spring-social-demo” but yours may be different. Take note of the App ID and App Secret, you’ll need them later.

Next, click Add Product on the left sidebar. Choose the Facebook Login product from the menu, and add it. Then open the settings for the Facebook Login product, found directly below the Facebook Login product on the left sidebar. In the Valid OAuth redirect URIs field, enter: http://localhost:8080/connect/facebook.

Add Product

And with that, your Facebook app is now ready to connect with Spring Social!

Create Personality Insights Service

Next we’ll setup a Watson Personality Insights service in IBM Cloud. Navigate to the IBM Cloud catalog and find the Personality Insights service. Create an instance of this service; I will use the default settings for this demo.

After your service has been provisioned, you will should be directed to its page. Click Service Credentials on the left sidebar. If a set of credentials has already been created, expand them to find your username and password. If you do not see a set of credentials on this page, click New Credentials and follow the prompts given.

Once you have found your credential set, take note of them, as we will need them for our Spring application.

Create Spring Social Application

Choose a directory for this project, and create a pom.xml file inside it with the following contents:

<br>
<?xml version="1.0" encoding="UTF-8"?><br>
<project xmlns="http://maven.apache.org/POM/4.0.0"<br>
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<br>
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><br>
    <modelVersion>4.0.0</modelVersion><p></p>
<p>    <groupId>com.example</groupId><br>
    <artifactId>spring-social-watson</artifactId><br>
    <version>1.0-SNAPSHOT</version></p>
<p>    <parent><br>
        <groupId>org.springframework.boot</groupId><br>
        <artifactId>spring-boot-starter-parent</artifactId><br>
        <version>1.5.7.RELEASE</version><br>
    </parent></p>
<p>    <dependencies><br>
        <dependency><br>
            <groupId>org.springframework.boot</groupId><br>
            <artifactId>spring-boot-starter-thymeleaf</artifactId><br>
        </dependency><br>
        <dependency><br>
            <groupId>org.springframework.social</groupId><br>
            <artifactId>spring-social-facebook</artifactId><br>
            <version>3.0.0.M1</version><br>
        </dependency><br>
        <dependency><br>
            <groupId>org.springframework.social</groupId><br>
            <artifactId>spring-social-core</artifactId><br>
            <version>2.0.0.M2</version><br>
        </dependency><br>
        <dependency><br>
            <groupId>org.springframework.social</groupId><br>
            <artifactId>spring-social-config</artifactId><br>
            <version>2.0.0.M2</version><br>
        </dependency><br>
        <dependency><br>
            <groupId>com.ibm.watson.developer_cloud</groupId><br>
            <artifactId>java-sdk</artifactId><br>
            <version>3.9.1</version><br>
        </dependency><br>
    </dependencies></p>
<p>    <properties><br>
        <java.version>1.8</java.version><br>
    </properties></p>
<p>    <build><br>
        <plugins><br>
            <plugin><br>
                <groupId>org.springframework.boot</groupId><br>
                <artifactId>spring-boot-maven-plugin</artifactId><br>
            </plugin><br>
        </plugins><br>
    </build></p>
<p>    <repositories><br>
        <repository><br>
            <id>spring-snapshots</id><br>
            <name>Spring Snapshots</name><br>
            <url>https://repo.spring.io/libs-milestone</url><br>
            <snapshots><br>
                <enabled>false</enabled><br>
            </snapshots><br>
        </repository><br>
    </repositories><br>
    <pluginRepositories><br>
        <pluginRepository><br>
            <id>spring-snapshots</id><br>
            <name>Spring Snapshots</name><br>
            <url>https://repo.spring.io/libs-milestone</url><br>
            <snapshots><br>
                <enabled>false</enabled><br>
            </snapshots><br>
        </pluginRepository><br>
    </pluginRepositories><br>
</project><br>
</p>

 

Before we can connect to Facebook and the Watson Personality Insights service, we need to provide the API credentials you have been accumulating. Create an application.properties file with the following contents:

<br>
spring.social.facebook.appId=**********<br>
spring.social.facebook.appSecret=**********<p></p>
<p>personalityinsights.user=**********<br>
personalityinsights.pass=**********<br>
</p>

You should, of course, fill in the App ID and App Secret from your Facebook app as well as the credentials from your Watson Personality Service on IBM Cloud here.

Next, let’s setup the Thymeleaf templates from the Spring guide. These files will provide the front end for our simple Spring Social application. For more information about these files, check out the Spring guide on Accessing Facebook Data.

<br>
<html><br>
<head><br>
    <title>Hello Facebook</title><br>
</head><br>
<body><br>
    <h3>Connect to Facebook</h3><p></p>
<p>    <form action="/connect/facebook" method="POST"><br>
        <input type="hidden" name="scope" value="user_posts" /><br>
        <div class="formInfo"><br>
            <p>You aren't connected to Facebook yet. Click the button to connect this application with your Facebook account.</p><br>
        </div><br>
        <p><button type="submit">Connect to Facebook</button></p><br>
    </form><br>
</body><br>
</html><br>
</p>

 

<br>
<html><br>
<head><br>
    <title>Hello Facebook</title><br>
</head><br>
<body><br>
    <h3>Connected to Facebook</h3><p></p>
<p>    <p><br>
        You are now connected to your Facebook account.<br>
        Click <a href="/">here</a> to see some entries from your Facebook feed.<br>
    </p><br>
</body><br>
</html><br>
</p>

 

<br>
<html xmlns:th="http://www.w3.org/1999/xhtml"><br>
<head><br>
    <title>Hello Facebook</title><br>
</head><br>
<body><br>
    <h3>Hello, <span th:text="${facebookProfile.name}">Some User</span>!</h3><p></p>
<p>    <h4>Here is your feed:</h4></p>
<p>    <div th:each="post:${feed}"><br>
        <b th:text="${post.from.name}">from</b> wrote:<br>
        <p th:text="${post.message}">message text</p><br>
        <img th:if="${post.picture}" th:src="${post.picture}"/><br>
        <hr/><br>
    </div><br>
</body><br>
</html><br>
</p>

Now we have a bunch of HTML files, so let’s setup a Controller to serve them. Create a MainController.java with the following contents:

<br>
package application;<p></p>
<p>import com.ibm.watson.developer_cloud.personality_insights.v3.PersonalityInsights;<br>
import org.springframework.beans.factory.annotation.Autowired;<br>
import org.springframework.social.connect.ConnectionRepository;<br>
import org.springframework.social.facebook.api.Facebook;<br>
import org.springframework.social.facebook.api.PagedList;<br>
import org.springframework.social.facebook.api.Post;<br>
import org.springframework.stereotype.Controller;<br>
import org.springframework.ui.Model;<br>
import org.springframework.web.bind.annotation.GetMapping;<br>
import org.springframework.web.bind.annotation.RequestMapping;</p>
<p>@Controller<br>
@RequestMapping("/")<br>
public class MainController {</p>
<p>    private Facebook facebook;<br>
    private ConnectionRepository connectionRepository;</p>
<p>    @Autowired<br>
    PersonalityInsights personalityInsights;</p>
<p>    public MainController(Facebook facebook, ConnectionRepository connectionRepository) {<br>
        this.facebook = facebook;<br>
        this.connectionRepository = connectionRepository;<br>
    }</p>
<p>    @GetMapping<br>
    public String facebookFeed(Model model) {<br>
        if (connectionRepository.findPrimaryConnection(Facebook.class) == null) {<br>
            return "redirect:/connect/facebook";<br>
        }<br>
        model.addAttribute("facebookProfile", facebook.userOperations().getUserProfile());<br>
        PagedList<Post> feed = facebook.feedOperations().getFeed();<br>
        model.addAttribute("feed", feed);</p>
<p>        String postText = "";<br>
        for(Post post : feed){<br>
            if(post.getMessage() != null){<br>
                postText += post.getMessage();<br>
            }<br>
        }</p>
<p>        System.out.println(personalityInsights.getProfile(postText).execute().toString());</p>
<p>        return "feed";<br>
    }<br>
}<br>
</p>

The Controller you just created will handle authentication into the Spring application via Facebook. If the user has not signed in, it redirects to the /connect/facebook page to allow Facebook signin. After the user has signed in with Facebook, the Controller accesses the feed from their profile.

At this point, we add the Watson Personality Insights service. Given the PagedList of Posts, we aggregate the text from the Facebook posts into a String. Then this String is passed to an instance of the PersonalityInsights object, and the result is printed to the command line.

The last thing the controller does is return the feed template, which will be populated with the post data from the PagedList we just used.

You may have notice that we Autowired our reference to the PersonalityInsights object for use in the Controller. Next let’s configure that behavior; create a PersonalityInsightsConfig.java file with the following contents:

<br>
package application;<p></p>
<p>import com.ibm.watson.developer_cloud.personality_insights.v3.PersonalityInsights;<br>
import org.springframework.beans.factory.annotation.Value;<br>
import org.springframework.context.annotation.Bean;<br>
import org.springframework.context.annotation.Configuration;</p>
<p>@Configuration<br>
public class PersonalityInsightsConfig {</p>
<p>    @Value("${personalityinsights.user}")<br>
    String user;<br>
    @Value("${personalityinsights.pass}")<br>
    String pass;</p>
<p>    @Bean<br>
    public PersonalityInsights personalityInsights(){<br>
        return new PersonalityInsights("2017-10-13", user, pass);<br>
    }<br>
}<br>
</p>

 

In this file, we setup a Spring Bean for the PersonalityInsights object. The credentials for the service you created are pulled from the application.properties file, where we previously put them.

Finally, let’s make the application executable. Create an Application.java with the following contents:

<br>
package application;<p></p>
<p>import org.springframework.boot.SpringApplication;<br>
import org.springframework.boot.autoconfigure.SpringBootApplication;</p>
<p>@SpringBootApplication<br>
public class Application {</p>
<p>    public static void main(String[] args) {<br>
        SpringApplication.run(Application.class, args);<br>
    }<br>
}<br>
</p>

 

That’s it! If you’ve followed along, you should have a Spring Social application that is ready to test out.

Testing the Spring Social Application

We are going to test our application locally. Start up the Spring Social app with the following command:

<br>
$ mvn spring-boot:run<br>

 

Now open your web browser and navigate to localhost:8080. Click the Connect to Facebook button. You should be redirected to Facebook and asked to login, if you aren’t already on your current browser. After logging into Facebook, your app will request some basic permissions need to access your profile feed. Accept these permission, then you will be redirected back to localhost.

Now click the given link to see your profile feed. After your feed populates the screen, check your terminal where the app is running. You should see a summary from the Watson Personality Insights service printed here, similar to the following:

<br>
{<br>
  "word_count": 1169,<br>
  "processed_language": "en",<br>
  "personality": [<br>
    {<br>
      "trait_id": "big5_openness",<br>
      "name": "Openness",<br>
      "category": "personality",<br>
      "percentile": 0.04629133759433651,<br>
      "children": [<br>
        {<br>
          "trait_id": "facet_adventurousness",<br>
          "name": "Adventurousness",<br>
          "category": "personality",<br>
          "percentile": 0.14207297321837886<br>
        },<br>
        {<br>
          "trait_id": "facet_artistic_interests",<br>
          "name": "Artistic interests",<br>
          "category": "personality",<br>
          "percentile": 0.6131870811015447<br>
        },<br>
        {<br>
          "trait_id": "facet_emotionality",<br>
          "name": "Emotionality",<br>
          "category": "personality",<br>
          "percentile": 0.31552338516435013<br>
        },<br>
        {<br>
          "trait_id": "facet_imagination",<br>
          "name": "Imagination",<br>
          "category": "personality",<br>
          "percentile": 0.9898232921594388<br>
        },<br>
        {<br>
          "trait_id": "facet_intellect",<br>
          "name": "Intellect",<br>
          "category": "personality",<br>
          "percentile": 0.6433438955906992<br>
        },<br>
        {<br>
          "trait_id": "facet_liberalism",<br>
          "name": "Authority-challenging",<br>
          "category": "personality",<br>
          "percentile": 0.6171632971598747<br>
        }<br>
      ]<br>
    },<br>
    {<br>
      "trait_id": "big5_conscientiousness",<br>
      "name": "Conscientiousness",<br>
      "category": "personality",<br>
      "percentile": 0.07700184530299642,<br>
      "children": [<br>
        {<br>
          "trait_id": "facet_achievement_striving",<br>
          "name": "Achievement striving",<br>
          "category": "personality",<br>
          "percentile": 0.38163952297297543<br>
        },<br>
        {<br>
          "trait_id": "facet_cautiousness",<br>
          "name": "Cautiousness",<br>
          "category": "personality",<br>
          "percentile": 0.19481907665762072<br>
        },<br>
        {<br>
          "trait_id": "facet_dutifulness",<br>
          "name": "Dutifulness",<br>
          "category": "personality",<br>
          "percentile": 5.326171765568377E-5<br>
        },<br>
        {<br>
          "trait_id": "facet_orderliness",<br>
          "name": "Orderliness",<br>
          "category": "personality",<br>
          "percentile": 0.09838750944841129<br>
        },<br>
        {<br>
          "trait_id": "facet_self_discipline",<br>
          "name": "Self-discipline",<br>
          "category": "personality",<br>
          "percentile": 0.1344430219343562<br>
        },<br>
        {<br>
          "trait_id": "facet_self_efficacy",<br>
          "name": "Self-efficacy",<br>
          "category": "personality",<br>
          "percentile": 0.9954949963176749<br>
        }<br>
      ]<br>
    },<br>
    {<br>
      "trait_id": "big5_extraversion",<br>
      "name": "Extraversion",<br>
      "category": "personality",<br>
      "percentile": 0.12766044810678484,<br>
      "children": [<br>
        {<br>
          "trait_id": "facet_activity_level",<br>
          "name": "Activity level",<br>
          "category": "personality",<br>
          "percentile": 0.5382740396281092<br>
        },<br>
        {<br>
          "trait_id": "facet_assertiveness",<br>
          "name": "Assertiveness",<br>
          "category": "personality",<br>
          "percentile": 0.7998407340496554<br>
        },<br>
        {<br>
          "trait_id": "facet_cheerfulness",<br>
          "name": "Cheerfulness",<br>
          "category": "personality",<br>
          "percentile": 0.047242866079222845<br>
        },<br>
        {<br>
          "trait_id": "facet_excitement_seeking",<br>
          "name": "Excitement-seeking",<br>
          "category": "personality",<br>
          "percentile": 0.9998659959434537<br>
        },<br>
        {<br>
          "trait_id": "facet_friendliness",<br>
          "name": "Outgoing",<br>
          "category": "personality",<br>
          "percentile": 0.03740158462434112<br>
        },<br>
        {<br>
          "trait_id": "facet_gregariousness",<br>
          "name": "Gregariousness",<br>
          "category": "personality",<br>
          "percentile": 0.045883367411304<br>
        }<br>
      ]<br>
    },<br>
    {<br>
      "trait_id": "big5_agreeableness",<br>
      "name": "Agreeableness",<br>
      "category": "personality",<br>
      "percentile": 0.09171247917680148,<br>
      "children": [<br>
        {<br>
          "trait_id": "facet_altruism",<br>
          "name": "Altruism",<br>
          "category": "personality",<br>
          "percentile": 0.1553546640159444<br>
        },<br>
        {<br>
          "trait_id": "facet_cooperation",<br>
          "name": "Cooperation",<br>
          "category": "personality",<br>
          "percentile": 0.005464467830358977<br>
        },<br>
        {<br>
          "trait_id": "facet_modesty",<br>
          "name": "Modesty",<br>
          "category": "personality",<br>
          "percentile": 0.050939995240209035<br>
        },<br>
        {<br>
          "trait_id": "facet_morality",<br>
          "name": "Uncompromising",<br>
          "category": "personality",<br>
          "percentile": 0.03002592357838718<br>
        },<br>
        {<br>
          "trait_id": "facet_sympathy",<br>
          "name": "Sympathy",<br>
          "category": "personality",<br>
          "percentile": 0.4876659161485788<br>
        },<br>
        {<br>
          "trait_id": "facet_trust",<br>
          "name": "Trust",<br>
          "category": "personality",<br>
          "percentile": 0.4901349497774661<br>
        }<br>
      ]<br>
    },<br>
    {<br>
      "trait_id": "big5_neuroticism",<br>
      "name": "Emotional range",<br>
      "category": "personality",<br>
      "percentile": 1.376105175914244E-4,<br>
      "children": [<br>
        {<br>
          "trait_id": "facet_anger",<br>
          "name": "Fiery",<br>
          "category": "personality",<br>
          "percentile": 0.9834014729591617<br>
        },<br>
        {<br>
          "trait_id": "facet_anxiety",<br>
          "name": "Prone to worry",<br>
          "category": "personality",<br>
          "percentile": 0.998564293415926<br>
        },<br>
        {<br>
          "trait_id": "facet_depression",<br>
          "name": "Melancholy",<br>
          "category": "personality",<br>
          "percentile": 0.9273971482374409<br>
        },<br>
        {<br>
          "trait_id": "facet_immoderation",<br>
          "name": "Immoderation",<br>
          "category": "personality",<br>
          "percentile": 0.1011839452079169<br>
        },<br>
        {<br>
          "trait_id": "facet_self_consciousness",<br>
          "name": "Self-consciousness",<br>
          "category": "personality",<br>
          "percentile": 0.9956107140647223<br>
        },<br>
        {<br>
          "trait_id": "facet_vulnerability",<br>
          "name": "Susceptible to stress",<br>
          "category": "personality",<br>
          "percentile": 0.9641357140105273<br>
        }<br>
      ]<br>
    }<br>
  ],<br>
  "needs": [<br>
    {<br>
      "trait_id": "need_challenge",<br>
      "name": "Challenge",<br>
      "category": "needs",<br>
      "percentile": 0.4345112849820838<br>
    },<br>
    {<br>
      "trait_id": "need_closeness",<br>
      "name": "Closeness",<br>
      "category": "needs",<br>
      "percentile": 0.6246624813618438<br>
    },<br>
    {<br>
      "trait_id": "need_curiosity",<br>
      "name": "Curiosity",<br>
      "category": "needs",<br>
      "percentile": 0.8260757399691849<br>
    },<br>
    {<br>
      "trait_id": "need_excitement",<br>
      "name": "Excitement",<br>
      "category": "needs",<br>
      "percentile": 0.6868793816033816<br>
    },<br>
    {<br>
      "trait_id": "need_harmony",<br>
      "name": "Harmony",<br>
      "category": "needs",<br>
      "percentile": 0.840914288996311<br>
    },<br>
    {<br>
      "trait_id": "need_ideal",<br>
      "name": "Ideal",<br>
      "category": "needs",<br>
      "percentile": 0.8437343835745614<br>
    },<br>
    {<br>
      "trait_id": "need_liberty",<br>
      "name": "Liberty",<br>
      "category": "needs",<br>
      "percentile": 0.5583733685320165<br>
    },<br>
    {<br>
      "trait_id": "need_love",<br>
      "name": "Love",<br>
      "category": "needs",<br>
      "percentile": 0.5734163346351142<br>
    },<br>
    {<br>
      "trait_id": "need_practicality",<br>
      "name": "Practicality",<br>
      "category": "needs",<br>
      "percentile": 0.9895237347582294<br>
    },<br>
    {<br>
      "trait_id": "need_self_expression",<br>
      "name": "Self-expression",<br>
      "category": "needs",<br>
      "percentile": 0.6012587887694335<br>
    },<br>
    {<br>
      "trait_id": "need_stability",<br>
      "name": "Stability",<br>
      "category": "needs",<br>
      "percentile": 0.5841790214694709<br>
    },<br>
    {<br>
      "trait_id": "need_structure",<br>
      "name": "Structure",<br>
      "category": "needs",<br>
      "percentile": 0.21508497980422348<br>
    }<br>
  ],<br>
  "values": [<br>
    {<br>
      "trait_id": "value_conservation",<br>
      "name": "Conservation",<br>
      "category": "values",<br>
      "percentile": 0.4634834577688918<br>
    },<br>
    {<br>
      "trait_id": "value_openness_to_change",<br>
      "name": "Openness to change",<br>
      "category": "values",<br>
      "percentile": 0.6094698189509631<br>
    },<br>
    {<br>
      "trait_id": "value_hedonism",<br>
      "name": "Hedonism",<br>
      "category": "values",<br>
      "percentile": 0.8406417352684337<br>
    },<br>
    {<br>
      "trait_id": "value_self_enhancement",<br>
      "name": "Self-enhancement",<br>
      "category": "values",<br>
      "percentile": 0.7453257612907007<br>
    },<br>
    {<br>
      "trait_id": "value_self_transcendence",<br>
      "name": "Self-transcendence",<br>
      "category": "values",<br>
      "percentile": 0.27147776591572015<br>
    }<br>
  ],<br>
  "warnings": []<br>
}<br>

 

Conclusion

In this post, we created a Spring Social application that authentication with Facebook. Then we used the profile feed from the logged-in Facebook user to produce a personality analysis using the Watson Personality Insights service on IBM Cloud.

Be the first to hear about news, product updates, and innovation from IBM Cloud