|
4 | 4 | import io.github.vrchatapi.auth.*; |
5 | 5 | import io.github.vrchatapi.api.*; |
6 | 6 | import io.github.vrchatapi.model.*; |
| 7 | +import java.io.BufferedReader; |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.InputStreamReader; |
7 | 10 |
|
8 | 11 | public class Demo { |
9 | 12 |
|
10 | | - public static void main(String[] args) throws ApiException { |
| 13 | + public static void main(String[] args) throws ApiException, IOException { |
11 | 14 | // Step 1. VRChat consists of several API's |
12 | 15 | // e.g. (WorldsApi, UsersApi, FilesApi, NotificationsApi, FriendsApi, etc...) |
13 | 16 | // Here we instantiate the Authentication API which is required for logging in. |
14 | 17 | ApiClient defaultClient = Configuration.getDefaultApiClient(); |
15 | 18 | AuthenticationApi authApi = new AuthenticationApi(defaultClient); |
16 | 19 |
|
17 | 20 | // Step 2. We begin with creating a Configuration |
18 | | - // This contains the username and password for authentication. |
| 21 | + // This contains the username and password for authentication, as well as a user agent. |
19 | 22 | HttpBasicAuth authHeader = (HttpBasicAuth) defaultClient.getAuthentication("authHeader"); |
20 | 23 | authHeader.setUsername("username"); |
21 | 24 | authHeader.setPassword("password"); |
| 25 | + defaultClient.setUserAgent("ExampleProgram/0.0.1 my@email.com"); |
| 26 | + |
22 | 27 |
|
23 | 28 | // Step 3. Call getCurrentUser on Authentication API. |
24 | | - // This logs you in if the user isn't already logged in. |
25 | | - CurrentUser result = authApi.getCurrentUser(); |
26 | | - System.out.println(result.getDisplayName()); |
27 | | - |
28 | | - System.exit(0); |
| 29 | + BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) ); |
| 30 | + try { |
| 31 | + authApi.getCurrentUser(); |
| 32 | + } |
| 33 | + catch (IllegalArgumentException e){ |
| 34 | + // Step 4. Verify using 2fa. |
| 35 | + if (e.toString().contains("emailOtp")){ |
| 36 | + System.out.println("2FA Email code:"); |
| 37 | + TwoFactorEmailCode code = new TwoFactorEmailCode(); |
| 38 | + code.setCode(reader.readLine()); |
| 39 | + authApi.verify2FAEmailCode(code); |
| 40 | + } else { |
| 41 | + System.out.println("2FA Authenticator code:"); |
| 42 | + TwoFactorAuthCode code = new TwoFactorAuthCode(); |
| 43 | + code.setCode(reader.readLine()); |
| 44 | + authApi.verify2FA(code); |
| 45 | + } |
| 46 | + } |
| 47 | + CurrentUser user = authApi.getCurrentUser(); |
| 48 | + System.out.println("Logged in as: " + user.getDisplayName()); |
29 | 49 | } |
30 | 50 |
|
31 | 51 | } |
0 commit comments