Java Spring
Twitter login

How to add Twitter login to a Java Spring application

FusionAuth provides a convenient and quick way to implement Twitter login in your Java Spring app

How-To > Java Spring > Twitter Login

Start implementing Twitter login by setting up FusionAuth

About Twitter login

Configuring Twitter login makes it easy for your users to log in using a Twitter account they already have. Instead of creating an account on your application, users click on a ‘Login with Twitter’ button and log in with their credentials on Twitter. After successfully doing so, they’ll arrive back at your site.

In this doc, you’ll learn how to add Twitter login functionality to your Java Spring application. You’ll use FusionAuth to easily add Twitter login.

1. Configure FusionAuth

First, set up FusionAuth and a sample Java Spring application. Please follow this tutorial to get that sample application set up.

All done? Great.

Let’s get Twitter login set up for your Java Spring application.

2. Enable Twitter login

Now, let’s enable Twitter login using FusionAuth. Create another FusionAuth script. To set up Twitter login, all you need to do is tweak FusionAuth configuration.

Go back to the fusionauth-setup directory:

cd ../setup-fusionauth

You can use the same client library and API key as you used when setting up FusionAuth and the sample Java Spring application. But here, you are configuring a different aspect, namely Twitter login.

Put the below code in src/main/java/io/fusionauth/example/

package io.fusionauth.example; import java.util.HashMap; import java.util.UUID; import com.inversoft.error.Errors; import; import io.fusionauth.client.FusionAuthClient; import io.fusionauth.domain.api.IdentityProviderRequest; import io.fusionauth.domain.api.IdentityProviderResponse; import io.fusionauth.domain.provider.TwitterApplicationConfiguration; import io.fusionauth.domain.provider.TwitterIdentityProvider; public class AddTwitterOAuth { public static void main(String[] args) { final String apiKey = System.getProperty("fusionauth.api.key"); final FusionAuthClient client = new FusionAuthClient(apiKey, "http://localhost:9011"); // create a twitter provider UUID clientId = UUID.fromString(Setup.APPLICATION_ID); TwitterIdentityProvider twitterIdentityProvider = new TwitterIdentityProvider(); twitterIdentityProvider.enabled = true; twitterIdentityProvider.buttonText = "Login With Twitter"; twitterIdentityProvider.consumerKey = "change-this-in-production-to-be-a-real-twitter-key"; twitterIdentityProvider.consumerSecret = "change-this-in-production-to-be-a-real-twitter-secret"; // enable it for our application twitterIdentityProvider.applicationConfiguration = new HashMap<UUID, TwitterApplicationConfiguration>(); TwitterApplicationConfiguration twitterApplicationConfiguration = new TwitterApplicationConfiguration(); twitterApplicationConfiguration.enabled = true; twitterIdentityProvider.applicationConfiguration.put(clientId, twitterApplicationConfiguration); IdentityProviderRequest twitterIdentityProviderRequest = new IdentityProviderRequest(twitterIdentityProvider); ClientResponse<IdentityProviderResponse, Errors> identityProviderResponse = client.createIdentityProvider(null, twitterIdentityProviderRequest); if (!identityProviderResponse.wasSuccessful()) { throw new RuntimeException("couldn't add twitter login"); } } }

Run this code:

mvn compile && mvn exec:java -Dexec.mainClass="io.fusionauth.example.AddTwitterOAuth" -Dfusionauth.api.key=<your API key>

You are all done! With this configuration code, you’ve enabled Twitter login for your Java Spring application.

Manual configuration of Twitter login

You can also configure Twitter login manually by logging into the FusionAuth admin user interface and navigating to settings, then identity providers, then add a new Twitter identity provider. Configure it, and make sure you enable it for your application.

Manually setting up Twitter login.
Manually setting up Twitter login.

3. Test Out Twitter login

Now, test to see that you have Twitter login enabled for your application. Close any other incognito windows you have open and open a new one. (This ensures you’ve logged out of the application.)

Visit the Java Spring application and log in again.

Now, on the login page, you should see a Twitter login button.

What the user sees with Twitter login enabled.
What the user sees with Twitter login enabled.

This is the default look and feel, but you can configure branding using themes.

Next steps for Twitter login

If you want to let your users truly login with Twitter, make sure you set up your Twitter Identity Provider with real client id and client secret values provided by Twitter. Read more in these instructions.

Learn more by visiting the FusionAuth developer docs

Java Spring How-Tos

Twitter Login How-Tos