How-tos

Analyzing Spring Social Facebook Data with Watson Personality Insights

Share this post:

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 App button. 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.

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:


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>spring-social-watson</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-facebook</artifactId>
<version>3.0.0.M1</version>
</dependency>
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-core</artifactId>
<version>2.0.0.M2</version>
</dependency>
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-config</artifactId>
<version>2.0.0.M2</version>
</dependency>
<dependency>
<groupId>com.ibm.watson.developer_cloud</groupId>
<artifactId>java-sdk</artifactId>
<version>3.9.1</version>
</dependency>
</dependencies>

<properties>
<java.version>1.8</java.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>

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:


spring.social.facebook.appId=**********
spring.social.facebook.appSecret=**********

personalityinsights.user=**********
personalityinsights.pass=**********

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.


<html>
<head>
<title>Hello Facebook</title>
</head>
<body>
<h3>Connect to Facebook</h3>

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


<html>
<head>
<title>Hello Facebook</title>
</head>
<body>
<h3>Connected to Facebook</h3>

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


<html xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<title>Hello Facebook</title>
</head>
<body>
<h3>Hello, <span th:text="${facebookProfile.name}">Some User</span>!</h3>

<h4>Here is your feed:</h4>

<div th:each="post:${feed}">
<b th:text="${post.from.name}">from</b> wrote:
<p th:text="${post.message}">message text</p>
<img th:if="${post.picture}" th:src="${post.picture}"/>
<hr/>
</div>
</body>
</html>

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:


package application;

import com.ibm.watson.developer_cloud.personality_insights.v3.PersonalityInsights;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.social.connect.ConnectionRepository;
import org.springframework.social.facebook.api.Facebook;
import org.springframework.social.facebook.api.PagedList;
import org.springframework.social.facebook.api.Post;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/")
public class MainController {

private Facebook facebook;
private ConnectionRepository connectionRepository;

@Autowired
PersonalityInsights personalityInsights;

public MainController(Facebook facebook, ConnectionRepository connectionRepository) {
this.facebook = facebook;
this.connectionRepository = connectionRepository;
}

@GetMapping
public String facebookFeed(Model model) {
if (connectionRepository.findPrimaryConnection(Facebook.class) == null) {
return "redirect:/connect/facebook";
}
model.addAttribute("facebookProfile", facebook.userOperations().getUserProfile());
PagedList<Post> feed = facebook.feedOperations().getFeed();
model.addAttribute("feed", feed);

String postText = "";
for(Post post : feed){
if(post.getMessage() != null){
postText += post.getMessage();
}
}

System.out.println(personalityInsights.getProfile(postText).execute().toString());

return "feed";
}
}

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:


package application;

import com.ibm.watson.developer_cloud.personality_insights.v3.PersonalityInsights;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class PersonalityInsightsConfig {

@Value("${personalityinsights.user}")
String user;
@Value("${personalityinsights.pass}")
String pass;

@Bean
public PersonalityInsights personalityInsights(){
return new PersonalityInsights("2017-10-13", user, pass);
}
}

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:


package application;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

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:


$ mvn spring-boot:run

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:


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

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.

More How-tos stories
May 7, 2019

We’ve Moved! The IBM Cloud Blog Has a New URL

In an effort better integrate the IBM Cloud Blog with the IBM Cloud web experience, we have migrated the blog to a new URL: www.ibm.com/cloud/blog.

Continue reading

May 6, 2019

Use IBM Cloud Certificate Manager to Obtain Let’s Encrypt TLS Certificates for Your Public Domains

IBM Cloud Certificate Manager now lets you obtain TLS certificates signed by Let’s Encrypt. Let’s Encrypt is an automated, ACME-protocol-based CA that issues free certificates valid for 90 days.

Continue reading

May 6, 2019

Are You Ready for SAP S/4HANA Running on Cloud?

Our clients tell us SAP applications are central to their success and strategy for cloud, with a deadline to refresh the business processes and move to SAP S/4HANA by 2025. Now is the time to assess, plan and execute the journey to cloud and SAP S/4HANA

Continue reading