Configure Bearer Token, OAuth 2.0 PKCE, or OAuth 1.0a in the Java XDK.
XClient supports Bearer Token (app-only), OAuth 2.0 with PKCE (user context), and OAuth 1.0a (user context). Get your credentials from the X Developer Console.
Bearer Token
import com.x.xdk.XClient;XClient client = XClient.builder() .bearerToken(System.getenv("X_BEARER_TOKEN")) .build();var user = client.users().getByUsername("XDevelopers");
OAuth 2.0 with PKCE
If you already have a user access token:
XClient client = XClient.builder() .accessToken(System.getenv("X_ACCESS_TOKEN")) .build();var me = client.users().getMe();
To run the PKCE authorization flow and refresh tokens with OAuth2Pkce:
import com.x.xdk.Schemas;import com.x.xdk.XClient;import com.x.xdk.auth.OAuth2Pkce;import com.x.xdk.auth.OAuth2Token;import java.util.List;OAuth2Pkce oauth2 = new OAuth2Pkce( System.getenv("X_CLIENT_ID"), System.getenv("X_CLIENT_SECRET"), // null for public clients "http://localhost:8080/callback", List.of("tweet.read", "tweet.write", "users.read", "offline.access"));// 1. Direct the user to authorize your appString authorizationUrl = oauth2.getAuthorizationUrl("random-csrf-state");// 2. Exchange the callback `code` parameter for tokensOAuth2Token token = oauth2.exchangeCode(callbackCode);// 3. Build XClient with the OAuth2Pkce instanceXClient client = XClient.builder() .oauth2(oauth2) .build();Schemas.CreatePostsResponse created = client.posts().create( new Schemas.CreatePostsRequest().text("Hello from Java XDK!"));// Refresh tokens when needed (requires offline.access scope)client.refreshToken();
OAuth 1.0a
OAuth 1.0a is disabled for newly created apps and is scheduled to be retired on January 31, 2027. Newly created apps must use OAuth 2.0 with PKCE or Bearer Token. Existing apps with OAuth 1.0a enabled can continue using it until January 31, 2027 — see Migrate from OAuth 1.0a to OAuth 2.0.