Java 6+ extensions to the Google OAuth Client Library providing verification code receivers and credential persistence for OAuth 2.0 flows
—
Interface and implementations for receiving OAuth 2.0 verification codes from users during the authorization process. These components handle the user interaction portion of OAuth 2.0 flows.
Core interface that defines how verification codes are received from users during OAuth 2.0 authorization flows.
/**
* OAuth 2.0 verification code receiver.
* Implementation should be thread-safe.
*/
public interface VerificationCodeReceiver {
/**
* Returns the redirect URI to be used in the authorization request.
* @return redirect URI string
* @throws IOException if URI generation fails
*/
String getRedirectUri() throws IOException;
/**
* Waits for a verification code from the user.
* This method blocks until a code is received.
* @return verification code string
* @throws IOException if code reception fails
*/
String waitForCode() throws IOException;
/**
* Releases any resources and stops any processes started.
* @throws IOException if resource cleanup fails
*/
void stop() throws IOException;
}Abstract implementation that prompts the user to manually enter the verification code via console input. Suitable for command-line applications and scenarios where automatic code reception is not possible.
/**
* OAuth 2.0 abstract verification code receiver that prompts user to paste the code
* copied from the browser. Implementation is thread-safe.
*/
public abstract class AbstractPromptReceiver implements VerificationCodeReceiver {
/**
* Prompts the user to enter the verification code via console input.
* Continues prompting until a non-empty code is entered.
* @return verification code entered by user
*/
@Override
public String waitForCode();
/**
* No-op implementation since no resources need to be stopped.
*/
@Override
public void stop();
// Abstract method - must be implemented by subclasses
/**
* Returns the redirect URI. Typically "urn:ietf:wg:oauth:2.0:oob" for out-of-band flows.
* @return redirect URI for the authorization request
* @throws IOException if URI generation fails
*/
public abstract String getRedirectUri() throws IOException;
}Usage Example:
import com.google.api.client.extensions.java6.auth.oauth2.AbstractPromptReceiver;
// Create a simple console-based receiver for out-of-band authorization
public class ConsoleReceiver extends AbstractPromptReceiver {
@Override
public String getRedirectUri() throws IOException {
// Out-of-band redirect URI for manual code entry
return "urn:ietf:wg:oauth:2.0:oob";
}
}
// Usage in authorization flow
ConsoleReceiver receiver = new ConsoleReceiver();
AuthorizationCodeInstalledApp app = new AuthorizationCodeInstalledApp(flow, receiver);
// When authorize() is called:
// 1. Browser opens to authorization URL
// 2. User grants permission and copies the code
// 3. Console prompts: "Please enter code: "
// 4. User pastes the code and presses Enter
// 5. Authorization completes
Credential credential = app.authorize("user-id");Custom Receiver Example:
import com.google.api.client.extensions.java6.auth.oauth2.VerificationCodeReceiver;
import java.util.Scanner;
// Custom receiver that uses a GUI dialog instead of console
public class DialogReceiver implements VerificationCodeReceiver {
private JDialog dialog;
private String code;
@Override
public String getRedirectUri() throws IOException {
return "urn:ietf:wg:oauth:2.0:oob";
}
@Override
public String waitForCode() throws IOException {
SwingUtilities.invokeLater(() -> {
dialog = new JDialog();
JTextField codeField = new JTextField(30);
JButton okButton = new JButton("OK");
okButton.addActionListener(e -> {
code = codeField.getText();
dialog.dispose();
});
dialog.add(new JLabel("Enter verification code:"));
dialog.add(codeField);
dialog.add(okButton);
dialog.setModal(true);
dialog.pack();
dialog.setVisible(true);
});
// Wait for dialog completion
while (code == null) {
Thread.sleep(100);
}
return code;
}
@Override
public void stop() throws IOException {
if (dialog != null) {
dialog.dispose();
}
}
}All VerificationCodeReceiver implementations should be thread-safe as specified in the interface documentation.
Always implement the stop() method to properly clean up resources:
Handle common error scenarios:
Common redirect URI patterns:
"urn:ietf:wg:oauth:2.0:oob" - Out-of-band (manual code entry)"http://localhost:8080" - Local HTTP server"http://localhost:0" - Local HTTP server with random portThe out-of-band (OOB) flow is commonly used with AbstractPromptReceiver:
urn:ietf:wg:oauth:2.0:oobThis flow is ideal for:
Install with Tessl CLI
npx tessl i tessl/maven-com-google-oauth-client--google-oauth-client-java6