OAuth Client for Java: ScribeJava and Spring Security OAuth2

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

OAuth Client for Java: ScribeJava and Spring Security OAuth2



In modern web development, integration with external services via the OAuth 2.0 protocol has become the de facto standard. Whether it's logging in through Google, GitHub, Facebook, or accessing third-party platform APIs, a Java developer needs a reliable tool for working with OAuth. In this article, we will take a detailed look at two key libraries: ScribeJava (lightweight and versatile) and Spring Security OAuth2 (part of the Spring ecosystem). You will learn how to choose the right tool and implement an OAuth client in Java.



What is an OAuth client and why is it needed?

An OAuth client is a software component that allows your application (the client) to securely access a user's resources on a third-party server (the provider) without transmitting a password. Instead, an exchange of access tokens is used. In Java, this is implemented through specialized libraries that handle all the complexity of the protocol: generating the authorization URL, processing the callback, and obtaining and refreshing tokens.



1. ScribeJava — A Lightweight and Independent OAuth Client

ScribeJava (formerly Scribe) is a minimalist yet powerful library for OAuth 1.0a and OAuth 2.0. It is not tied to any framework, making it ideal for microservices, standard Java applications, or Android. Key advantages:

  • Support for dozens of providers out of the box (Google, Facebook, GitHub, VK, Yandex, etc.)
  • Simple and intuitive API
  • No unnecessary dependencies
  • Support for PKCE (Proof Key for Code Exchange) for public clients


Example: Authorization via GitHub using ScribeJava

Let's consider a basic scenario — obtaining an access token from GitHub. First, add the dependency to your pom.xml:



<dependency>    <groupId>com.github.scribejava</groupId>    <artifactId>scribejava-apis</artifactId>    <version>8.3.3</version></dependency>


Now the OAuth client code:



import com.github.scribejava.apis.GitHubApi;import com.github.scribejava.core.builder.ServiceBuilder;import com.github.scribejava.core.model.OAuth2AccessToken;import com.github.scribejava.core.model.OAuthRequest;import com.github.scribejava.core.model.Response;import com.github.scribejava.core.model.Verb;import com.github.scribejava.core.oauth.OAuth20Service;

public class GitHubOAuthExample { public static void main(String[] args) throws Exception { final String clientId = "your_client_id"; final String clientSecret = "your_client_secret"; final String callbackUrl = "http://localhost:8080/callback";

// Create the OAuth 2.0 service OAuth20Service service = new ServiceBuilder(clientId) .apiSecret(clientSecret) .callback(callbackUrl) .scope("user:email") // request access to email .build(GitHubApi.instance());

// Step 1: Get the URL for user redirect final String authorizationUrl = service.getAuthorizationUrl(); System.out.println("Follow the link for authorization: " + authorizationUrl);

// Step 2: After redirect, extract the code from the callback // (This is usually done in a controller; here for example — console input) System.out.print("Enter the code from the callback: "); java.util.Scanner scanner = new java.util.Scanner(System.in); String code = scanner.nextLine();

// Step 3: Exchange the code for an access token OAuth2AccessToken accessToken = service.getAccessToken(code); System.out.println("Access Token: " + accessToken.getAccessToken());

// Step 4: Use the token to make an API request OAuthRequest request = new OAuthRequest(Verb.GET, "https://api.g

Recommendations