Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve examples #3

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 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");
authHeader.setUsername("username");
authHeader.setPassword("password");
defaultClient.setUserAgent("ExampleProgram/0.0.1 [email protected]");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example is inconsistently formatted.



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

}
Loading