Skip to content

Commit bfd3b8f

Browse files
authored
Update documentation (#11)
* Improve example * Update example.js * Remove comma
1 parent adfaa07 commit bfd3b8f

File tree

2 files changed

+50
-11
lines changed

2 files changed

+50
-11
lines changed

README.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,36 @@ npm install vrchat
2626
Below is an example on how to login to the API and fetch your own user information.
2727

2828
```javascript
29-
// Step 1. We begin with creating a Configuration, which contains the username and password for authentication.
29+
// Step 1. We begin with creating a Configuration, which contains the username and password for authentication, as well as an options dictionary, which contains the user agent header.
3030
const vrchat = require("vrchat");
31+
3132
const configuration = new vrchat.Configuration({
3233
username: "username",
3334
password: "password"
3435
});
3536

37+
const options = { headers: { "User-Agent": "ExampleProgram/0.0.1 [email protected]"}};
38+
3639
// Step 2. VRChat consists of several API's (WorldsApi, UsersApi, FilesApi, NotificationsApi, FriendsApi, etc...)
3740
// Here we instantiate the Authentication API which is required for logging in.
3841
const AuthenticationApi = new vrchat.AuthenticationApi(configuration);
3942

4043
// Step 3. Calling getCurrentUser on Authentication API logs you in if the user isn't already logged in.
41-
AuthenticationApi.getCurrentUser().then(resp => {
42-
const currentUser = resp.data;
44+
AuthenticationApi.getCurrentUser(options).then(async resp => {
45+
var currentUser = resp.data;
46+
47+
// Step 3.5. Calling email verify2fa if the account has 2FA disabled
48+
if (currentUser["requiresTwoFactorAuth"] && currentUser["requiresTwoFactorAuth"][0] === "emailOtp") {
49+
await AuthenticationApi.verify2FAEmailCode({ code: "123456" }, options)
50+
currentUser = (await AuthenticationApi.getCurrentUser(options)).data;
51+
}
52+
53+
// Step 3.5. Calling verify2fa if the account has 2FA enabled
54+
if (currentUser["requiresTwoFactorAuth"] && currentUser["requiresTwoFactorAuth"][0] === "totp") {
55+
await AuthenticationApi.verify2FA({ code: "123456" }, options)
56+
currentUser = (await AuthenticationApi.getCurrentUser(options)).data;
57+
}
58+
4359
console.log(`Logged in as: ${currentUser.displayName}`);
4460
});
4561
```

example.js

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,45 @@
11
const vrchat = require("vrchat");
2+
const readline = require("readline")
3+
4+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
5+
const prompt = (query) => new Promise((resolve) => rl.question(query, resolve));
6+
7+
28
const configuration = new vrchat.Configuration({
39
username: "username",
410
password: "password"
511
});
612

13+
const options = { headers: { "User-Agent": "ExampleProgram/0.0.1 [email protected]"}};
714
const AuthenticationApi = new vrchat.AuthenticationApi(configuration);
815
const UsersApi = new vrchat.UsersApi(configuration);
916
const SystemApi = new vrchat.SystemApi(configuration);
1017

11-
SystemApi.getCurrentOnlineUsers().then(resp => {
12-
console.log(`Current Online Users: ${resp.data}`);
18+
AuthenticationApi.getCurrentUser(options).then(async resp => {
19+
var currentUser = resp.data;
20+
21+
if (currentUser["requiresTwoFactorAuth"] && currentUser["requiresTwoFactorAuth"][0] === "emailOtp") {
22+
await AuthenticationApi.verify2FAEmailCode({ code: await prompt("email Code\n") }, options)
23+
currentUser = (await AuthenticationApi.getCurrentUser(options)).data;
24+
}
25+
if (currentUser["requiresTwoFactorAuth"] && currentUser["requiresTwoFactorAuth"][0] === "totp") {
26+
await AuthenticationApi.verify2FA({ code: await prompt("2fa Code\n") }, options)
27+
currentUser = (await AuthenticationApi.getCurrentUser(options)).data;
28+
}
1329

14-
// Calling getCurrentUser on Authentication API logs you in if the user isn't already logged in.
15-
AuthenticationApi.getCurrentUser().then(resp => {
16-
console.log(`Logged in as: ${resp.data.displayName}`);
30+
console.log(`Logged in as: ${currentUser.displayName}`);
1731

18-
UsersApi.getUser("usr_c1644b5b-3ca4-45b4-97c6-a2a0de70d469").then(resp => {
19-
console.log(resp.data.displayName); // Should print out "tupper"
32+
SystemApi.getCurrentOnlineUsers(options).then(resp => {
33+
console.log(`Current Online Users: ${resp.data}`);
34+
35+
// Calling getCurrentUser on Authentication API logs you in if the user isn't already logged in.
36+
AuthenticationApi.getCurrentUser(options).then(resp => {
37+
console.log(`Logged in as: ${resp.data.displayName}`);
38+
39+
UsersApi.getUser("usr_c1644b5b-3ca4-45b4-97c6-a2a0de70d469", options).then(resp => {
40+
console.log(resp.data.displayName); // Should print out "tupper"
41+
});
2042
});
2143
});
22-
});
44+
});
45+

0 commit comments

Comments
 (0)