Java 6+ extensions to the Google OAuth Client Library providing verification code receivers and credential persistence for OAuth 2.0 flows
npx @tessl/cli install tessl/maven-com-google-oauth-client--google-oauth-client-java6@1.39.0Google OAuth Client Java 6 provides Java 6+ extensions to the Google OAuth Client Library for Java. It focuses on OAuth 2.0 verification code receivers and credential persistence mechanisms for installed applications, command-line tools, and desktop applications that need to handle OAuth 2.0 authorization flows.
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-java6</artifactId>
<version>1.39.0</version>
</dependency>import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.java6.auth.oauth2.VerificationCodeReceiver;
import com.google.api.client.extensions.java6.auth.oauth2.AbstractPromptReceiver;
import com.google.api.client.extensions.java6.auth.oauth2.FileCredentialStore;
import com.google.api.client.auth.oauth2.AuthorizationCodeFlow;
import com.google.api.client.auth.oauth2.Credential;import com.google.api.client.auth.oauth2.AuthorizationCodeFlow;
import com.google.api.client.auth.oauth2.BearerToken;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.auth.oauth2.ClientParametersAuthentication;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.java6.auth.oauth2.AbstractPromptReceiver;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.json.JsonFactory;
// Create a custom prompt receiver
class ConsoleReceiver extends AbstractPromptReceiver {
@Override
public String getRedirectUri() {
return "urn:ietf:wg:oauth:2.0:oob";
}
}
// Set up OAuth flow for installed application
AuthorizationCodeFlow flow = new AuthorizationCodeFlow.Builder(
BearerToken.authorizationHeaderAccessMethod(),
httpTransport, // Your HttpTransport instance
jsonFactory, // Your JsonFactory instance
new GenericUrl("https://oauth2.googleapis.com/token"), // Token server URL
new ClientParametersAuthentication("your-client-id", "your-client-secret"), // Client authentication
"your-client-id", // Your OAuth 2.0 client ID
"https://accounts.google.com/o/oauth2/auth") // Authorization server URL
.build();
// Authorize user
ConsoleReceiver receiver = new ConsoleReceiver();
AuthorizationCodeInstalledApp app = new AuthorizationCodeInstalledApp(flow, receiver);
Credential credential = app.authorize("user123");Google OAuth Client Java 6 is built around several key components:
Complete OAuth 2.0 authorization flow implementation for installed applications, including browser integration and credential management.
public class AuthorizationCodeInstalledApp {
public AuthorizationCodeInstalledApp(
AuthorizationCodeFlow flow,
VerificationCodeReceiver receiver);
public AuthorizationCodeInstalledApp(
AuthorizationCodeFlow flow,
VerificationCodeReceiver receiver,
Browser browser);
public Credential authorize(String userId) throws IOException;
public final AuthorizationCodeFlow getFlow();
public final VerificationCodeReceiver getReceiver();
public static void browse(String url);
}Interface and implementations for receiving OAuth 2.0 verification codes from users during the authorization process.
public interface VerificationCodeReceiver {
String getRedirectUri() throws IOException;
String waitForCode() throws IOException;
void stop() throws IOException;
}
public abstract class AbstractPromptReceiver implements VerificationCodeReceiver {
public String waitForCode();
public void stop();
public abstract String getRedirectUri() throws IOException;
}
// Browser interface for custom browser implementations
public static interface Browser {
public void browse(String url) throws IOException;
}
public static class DefaultBrowser implements Browser {
@Override
public void browse(String url) throws IOException;
}Legacy file-based credential storage system for persisting OAuth 2.0 credentials. These components are deprecated in favor of FileDataStoreFactory.
@Deprecated
public class FileCredentialStore implements CredentialStore {
public FileCredentialStore(File file, JsonFactory jsonFactory) throws IOException;
public void store(String userId, Credential credential) throws IOException;
public boolean load(String userId, Credential credential);
public void delete(String userId, Credential credential) throws IOException;
}The file-based credential storage classes (FileCredentialStore, FilePersistedCredential, FilePersistedCredentials) are deprecated. Use FileDataStoreFactory with StoredCredential instead:
// Deprecated approach
FileCredentialStore store = new FileCredentialStore(file, jsonFactory);
// Recommended approach
FileDataStoreFactory dataStoreFactory = new FileDataStoreFactory(dataDirectory);
DataStore<StoredCredential> dataStore = StoredCredential.getDefaultDataStore(dataStoreFactory);