diff --git a/README.md b/README.md index 5fec0c1d..ef243cf0 100644 --- a/README.md +++ b/README.md @@ -49,22 +49,42 @@ dependencies { Below is an example on how to login to the API and fetch your own user information. ```java -// Step 1. VRChat consists of several API's + // Step 1. VRChat consists of several API's // e.g. (WorldsApi, UsersApi, FilesApi, NotificationsApi, FriendsApi, etc...) // Here we instantiate the Authentication API which is required for logging in. ApiClient defaultClient = Configuration.getDefaultApiClient(); AuthenticationApi authApi = new AuthenticationApi(defaultClient); // Step 2. We begin with creating a Configuration -// This contains the username and password for authentication. +// This contains the username and password for authentication, as well as a user agent. HttpBasicAuth authHeader = (HttpBasicAuth) defaultClient.getAuthentication("authHeader"); authHeader.setUsername("username"); authHeader.setPassword("password"); +defaultClient.setUserAgent("ExampleProgram/0.0.1 my@email.com"); + // Step 3. Call getCurrentUser on Authentication API. -// This logs you in if the user isn't already logged in. -CurrentUser result = authApi.getCurrentUser(); -System.out.println(result.getDisplayName()); +BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) ); +try { + authApi.getCurrentUser(); +} +catch (IllegalArgumentException e){ + // Step 4. Verify using 2fa. + if (e.toString().contains("emailOtp")){ + System.out.println("2FA Email code:"); + TwoFactorEmailCode code = new TwoFactorEmailCode(); + code.setCode(reader.readLine()); + authApi.verify2FAEmailCode(code); + } else { + System.out.println("2FA Authenticator code:"); + TwoFactorAuthCode code = new TwoFactorAuthCode(); + code.setCode(reader.readLine()); + authApi.verify2FA(code); + } +} + +CurrentUser user = authApi.getCurrentUser(); +System.out.println("Logged in as: " + user.getDisplayName()); ``` See [examples](https://github.com/vrchatapi/vrchatapi-java/blob/master/examples) for more example usage on getting started. diff --git a/examples/pom.xml b/examples/pom.xml index fb11611b..2d8e9c54 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -23,7 +23,12 @@ com.github.vrchatapi vrchatapi-java - 1.0.1-globalcookie3 + LATEST + + + com.squareup.okhttp3 + okhttp + 4.10.0 \ No newline at end of file diff --git a/examples/src/main/java/io/github/vrchatapi/demo/CookiesLoad.java b/examples/src/main/java/io/github/vrchatapi/demo/CookiesLoad.java new file mode 100644 index 00000000..77c80ac7 --- /dev/null +++ b/examples/src/main/java/io/github/vrchatapi/demo/CookiesLoad.java @@ -0,0 +1,25 @@ +package io.github.vrchatapi.demo; + +import io.github.vrchatapi.ApiClient; +import io.github.vrchatapi.ApiException; +import io.github.vrchatapi.Configuration; +import io.github.vrchatapi.api.AuthenticationApi; +import io.github.vrchatapi.auth.HttpBasicAuth; +import io.github.vrchatapi.model.CurrentUser; + +public class CookiesLoad { + + public static void main(String[] args) throws ApiException { + ApiClient defaultClient = Configuration.getDefaultApiClient(); + AuthenticationApi authApi = new AuthenticationApi(defaultClient); + HttpBasicAuth authHeader = (HttpBasicAuth) defaultClient.getAuthentication("authHeader"); + authHeader.setUsername("username"); + authHeader.setPassword("password"); + defaultClient.setUserAgent("ExampleProgram/0.0.1 my@email.com"); + defaultClient.addDefaultHeader("Cookie", "auth=[AUTH_COOKIE_HERE]; twoFactorAuth=[TWO_FACTOR_AUTH_COOKIE_HERE]"); + + CurrentUser user = authApi.getCurrentUser(); + System.out.println("Logged in as: " + user.getDisplayName()); + } + +} diff --git a/examples/src/main/java/io/github/vrchatapi/demo/CookiesStore.java b/examples/src/main/java/io/github/vrchatapi/demo/CookiesStore.java new file mode 100644 index 00000000..9c00192a --- /dev/null +++ b/examples/src/main/java/io/github/vrchatapi/demo/CookiesStore.java @@ -0,0 +1,52 @@ +package io.github.vrchatapi.demo; + +import io.github.vrchatapi.ApiClient; +import io.github.vrchatapi.ApiException; +import io.github.vrchatapi.Configuration; +import io.github.vrchatapi.api.AuthenticationApi; +import io.github.vrchatapi.auth.HttpBasicAuth; +import io.github.vrchatapi.model.CurrentUser; +import io.github.vrchatapi.model.TwoFactorAuthCode; +import io.github.vrchatapi.model.TwoFactorEmailCode; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.List; +import okhttp3.Cookie; + +public class CookiesStore { + + public static void main(String[] args) throws ApiException, IOException { + ApiClient defaultClient = Configuration.getDefaultApiClient(); + AuthenticationApi authApi = new AuthenticationApi(defaultClient); + HttpBasicAuth authHeader = (HttpBasicAuth) defaultClient.getAuthentication("authHeader"); + authHeader.setUsername("username"); + authHeader.setPassword("password"); + defaultClient.setUserAgent("ExampleProgram/0.0.1 my@email.com"); + + BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) ); + try { + authApi.getCurrentUser(); + } + catch (IllegalArgumentException e){ + if (e.toString().contains("emailOtp")){ + System.out.println("2FA Email code:"); + TwoFactorEmailCode code = new TwoFactorEmailCode(); + code.setCode(reader.readLine()); + authApi.verify2FAEmailCode(code); + } else { + System.out.println("2FA Authenticator code:"); + TwoFactorAuthCode code = new TwoFactorAuthCode(); + code.setCode(reader.readLine()); + authApi.verify2FA(code); + } + } + CurrentUser user = authApi.getCurrentUser(); + System.out.println("Logged in as: " + user.getDisplayName()); + + List cookies = defaultClient.getHttpClient().cookieJar().loadForRequest(new okhttp3.HttpUrl.Builder().scheme("http").host("api.vrchat.cloud").build()); + System.out.println("auth: " + cookies.get(0).value()); + System.out.println("twoFactorAuth: " + cookies.get(1).value()); + } + +} diff --git a/examples/src/main/java/io/github/vrchatapi/demo/Demo.java b/examples/src/main/java/io/github/vrchatapi/demo/Demo.java index 5e49ffa6..35573875 100644 --- a/examples/src/main/java/io/github/vrchatapi/demo/Demo.java +++ b/examples/src/main/java/io/github/vrchatapi/demo/Demo.java @@ -4,10 +4,13 @@ import io.github.vrchatapi.auth.*; import io.github.vrchatapi.api.*; import io.github.vrchatapi.model.*; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; public class Demo { - public static void main(String[] args) throws ApiException { + public static void main(String[] args) throws ApiException, IOException { // Step 1. VRChat consists of several API's // e.g. (WorldsApi, UsersApi, FilesApi, NotificationsApi, FriendsApi, etc...) // Here we instantiate the Authentication API which is required for logging in. @@ -15,17 +18,34 @@ public static void main(String[] args) throws ApiException { AuthenticationApi authApi = new AuthenticationApi(defaultClient); // Step 2. We begin with creating a Configuration - // This contains the username and password for authentication. + // This contains the username and password for authentication, as well as a user agent. HttpBasicAuth authHeader = (HttpBasicAuth) defaultClient.getAuthentication("authHeader"); authHeader.setUsername("username"); authHeader.setPassword("password"); + defaultClient.setUserAgent("ExampleProgram/0.0.1 my@email.com"); + // Step 3. Call getCurrentUser on Authentication API. - // This logs you in if the user isn't already logged in. - CurrentUser result = authApi.getCurrentUser(); - System.out.println(result.getDisplayName()); - - System.exit(0); + BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) ); + try { + authApi.getCurrentUser(); + } + catch (IllegalArgumentException e){ + // Step 4. Verify using 2fa. + if (e.toString().contains("emailOtp")){ + System.out.println("2FA Email code:"); + TwoFactorEmailCode code = new TwoFactorEmailCode(); + code.setCode(reader.readLine()); + authApi.verify2FAEmailCode(code); + } else { + System.out.println("2FA Authenticator code:"); + TwoFactorAuthCode code = new TwoFactorAuthCode(); + code.setCode(reader.readLine()); + authApi.verify2FA(code); + } + } + CurrentUser user = authApi.getCurrentUser(); + System.out.println("Logged in as: " + user.getDisplayName()); } }