A lightweight Java library for integrating RNet OAuth and AI Provider services. This library allows users to authenticate via RNet OAuth and pay for AI model token costs directly using their RNet account.
- OAuth2 PKCE Support: Secure authorization code flow with automatic code verifier and challenge generation.
- Token Management: Exchange authorization codes for tokens and refresh expired tokens.
- UserInfo Endpoint: Fetch the authenticated user's RNet profile with an access token.
- AI Integration: Easy methods to chat with AI models using standard or streaming responses.
- Lightweight: Minimal dependencies (Jackson for JSON, SLF4J for logging).
- Modern Java: Built for Java 25.
Add the following to your pom.xml:
<dependency>
<groupId>io.github.rnetai</groupId>
<artifactId>rnet-oauth</artifactId>
<version>1.0.0</version>
</dependency>RNetConfig config = new RNetConfig("client-id", "client-secret", "redirect-uri");
RNetAuth auth = new RNetAuth(config);
RNetAi ai = new RNetAi(config);// Generate PKCE values
PKCEUtil.PKCE pkce = auth.generatePKCE();
String codeVerifier = pkce.getVerifier(); // Store this in user session
// Get the URL to redirect the user to
// challenge: PKCE code challenge (optional)
// state: An optional string to maintain state between the request and callback (recommended for security)
String authUrl = auth.getAuthorizationUrl(pkce.getChallenge(), "optional-state");// After the user redirects back with a ?code=...
Map<String, Object> tokenResponse = auth.exchangeCodeForToken(authCode, codeVerifier);
String accessToken = (String) tokenResponse.get("access_token");
String refreshToken = (String) tokenResponse.get("refresh_token");Map<String, Object> refreshedTokens = auth.refreshAccessToken(refreshToken);
String newAccessToken = (String) refreshedTokens.get("access_token");Map<String, Object> userInfo = auth.getUserInfo(accessToken);
System.out.println(userInfo.get("email"));
System.out.println(userInfo.get("name"));The UserInfo response comes from RNet's /userinfo endpoint and may include:
sub, email, email_verified, name, preferred_username, user_id, role, and status.
Map<String, Object> payload = Map.of(
"contents", List.of(
Map.of(
"role", "user",
"parts", List.of(Map.of("text", "Hello!"))
)
)
);
Map<String, Object> response = ai.chat(payload, accessToken, "gemini-2.5-flash-lite");
System.out.println(response);InputStream stream = ai.chatStream(payload, accessToken, "gemini-2.5-flash-lite");
// Process the stream...This project is licensed under the MIT License.