Skip to content

Commit 2f31e8b

Browse files
authored
Improve examples (#13)
* Use baseOptions * Refactor example and add cookie examples
1 parent ef0fc2c commit 2f31e8b

File tree

3 files changed

+82
-22
lines changed

3 files changed

+82
-22
lines changed

cookies_load.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const vrchat = require("vrchat");
2+
3+
const configuration = new vrchat.Configuration({
4+
username: "username",
5+
password: "password",
6+
baseOptions: {
7+
headers: {
8+
"User-Agent": "ExampleProgram/0.0.1 [email protected]"
9+
"Cookie": "auth=[AUTH_COOKIE_HERE]; twoFactorAuth=[TWO_FACTOR_AUTH_COOKIE_HERE]}"
10+
}
11+
}
12+
});
13+
14+
const AuthenticationApi = new vrchat.AuthenticationApi(configuration);
15+
16+
async function main() {
17+
const currentUser = (await AuthenticationApi.getCurrentUser()).data;
18+
console.log(`Logged in as: ${currentUser.displayName}`);
19+
}
20+
21+
main();

cookies_store.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const vrchat = require("vrchat");
2+
3+
const readline = require("readline")
4+
import globalAxios from "axios"
5+
6+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
7+
const prompt = (query) => new Promise((resolve) => rl.question(query, resolve));
8+
9+
10+
const configuration = new vrchat.Configuration({
11+
username: "username",
12+
password: "password",
13+
baseOptions: {
14+
headers: { "User-Agent": "ExampleProgram/0.0.1 [email protected]"}
15+
}
16+
});
17+
18+
19+
const authenticationApi = new AuthenticationApi(configuration);
20+
21+
async function main() {
22+
var currentUser = (await authenticationApi.getCurrentUser()).data
23+
24+
if (currentUser["requiresTwoFactorAuth"] && currentUser["requiresTwoFactorAuth"][0] === "emailOtp") {
25+
await authenticationApi.verify2FAEmailCode({ code: await prompt("email Code\n") })
26+
currentUser = (await authenticationApi.getCurrentUser()).data;
27+
}
28+
if (currentUser["requiresTwoFactorAuth"] && currentUser["requiresTwoFactorAuth"][0] === "totp") {
29+
await authenticationApi.verify2FA({ code: await prompt("2fa Code\n") })
30+
currentUser = (await authenticationApi.getCurrentUser()).data;
31+
}
32+
33+
console.log(`Logged in as: ${currentUser.displayName}`);
34+
35+
const store = globalAxios.defaults.jar.store.idx["api.vrchat.cloud"]["/"];
36+
console.log(`auth=${store["auth"]["value"]}`)
37+
console.log(`twoFactorAuth=${store["twoFactorAuth"]["value"]}`)
38+
}
39+
40+
main();

example.js

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,38 @@ const prompt = (query) => new Promise((resolve) => rl.question(query, resolve));
77

88
const configuration = new vrchat.Configuration({
99
username: "username",
10-
password: "password"
10+
password: "password",
11+
baseOptions: {
12+
headers: { "User-Agent": "ExampleProgram/0.0.1 [email protected]"}
13+
}
1114
});
1215

13-
const options = { headers: { "User-Agent": "ExampleProgram/0.0.1 [email protected]"}};
14-
const AuthenticationApi = new vrchat.AuthenticationApi(configuration);
15-
const UsersApi = new vrchat.UsersApi(configuration);
16-
const SystemApi = new vrchat.SystemApi(configuration);
16+
const authenticationApi = new vrchat.AuthenticationApi(configuration);
17+
const usersApi = new vrchat.UsersApi(configuration);
18+
const systemApi = new vrchat.SystemApi(configuration);
1719

18-
AuthenticationApi.getCurrentUser(options).then(async resp => {
19-
var currentUser = resp.data;
20+
async function main() {
21+
var currentUser = (await authenticationApi.getCurrentUser()).data;
2022

2123
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+
await authenticationApi.verify2FAEmailCode({ code: await prompt("email Code\n") })
25+
currentUser = (await authenticationApi.getCurrentUser()).data;
2426
}
2527
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+
await authenticationApi.verify2FA({ code: await prompt("2fa Code\n") })
29+
currentUser = (await authenticationApi.getCurrentUser()).data;
2830
}
2931

3032
console.log(`Logged in as: ${currentUser.displayName}`);
3133

32-
SystemApi.getCurrentOnlineUsers(options).then(resp => {
33-
console.log(`Current Online Users: ${resp.data}`);
34+
const currentOnlineUsers = (await systemApi.getCurrentOnlineUsers()).data;
35+
console.log(`Current Online Users: ${resp.data}`);
36+
37+
const tupperUser = (await usersApi.getUser("usr_c1644b5b-3ca4-45b4-97c6-a2a0de70d469")).data;
38+
console.log(resp.data.displayName);
39+
}
40+
41+
main();
3442

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}`);
3843

39-
UsersApi.getUser("usr_c1644b5b-3ca4-45b4-97c6-a2a0de70d469", options).then(resp => {
40-
console.log(resp.data.displayName); // Should print out "tupper"
41-
});
42-
});
43-
});
44-
});
4544

0 commit comments

Comments
 (0)