-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from vrchatapi/feat/improve-examples
Improve examples
- Loading branch information
Showing
5 changed files
with
135 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
examples/src/main/java/io/github/vrchatapi/demo/CookiesLoad.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
52
examples/src/main/java/io/github/vrchatapi/demo/CookiesStore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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()); | ||
} | ||
|
||
} |