Skip to content

Commit

Permalink
Merge pull request #3 from vrchatapi/feat/improve-examples
Browse files Browse the repository at this point in the history
Improve examples
  • Loading branch information
jellejurre authored Nov 25, 2024
2 parents 19f3d06 + 150bc72 commit 96b24f4
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 13 deletions.
30 changes: 25 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 [email protected]");


// 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.
Expand Down
7 changes: 6 additions & 1 deletion examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@
<dependency>
<groupId>com.github.vrchatapi</groupId>
<artifactId>vrchatapi-java</artifactId>
<version>1.0.1-globalcookie3</version>
<version>LATEST</version>
</dependency>
<dependency> <!-- needed for cookie retrieval -->
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.10.0</version>
</dependency>
</dependencies>
</project>
25 changes: 25 additions & 0 deletions examples/src/main/java/io/github/vrchatapi/demo/CookiesLoad.java
Original file line number Diff line number Diff line change
@@ -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 [email protected]");
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());
}

}
52 changes: 52 additions & 0 deletions examples/src/main/java/io/github/vrchatapi/demo/CookiesStore.java
Original file line number Diff line number Diff line change
@@ -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 [email protected]");

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<Cookie> 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());
}

}
34 changes: 27 additions & 7 deletions examples/src/main/java/io/github/vrchatapi/demo/Demo.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,48 @@
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.
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 [email protected]");


// 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());
}

}

0 comments on commit 96b24f4

Please sign in to comment.