-
Notifications
You must be signed in to change notification settings - Fork 7
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"); | ||
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()); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.