-
Notifications
You must be signed in to change notification settings - Fork 10
/
example.js
58 lines (46 loc) · 2.31 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const PSN = require('./index.js');
// psn object accept optional params when construct.
const psn = new PSN({
lang: "en", //(default value en)
region: "hk", // server region(default value us)
refresh_token: null, //refresh_token(default value null)
access_token: null //access_token(default value null)
});
const npsso = "put your npsso code here in string form";
async function main() {
try {
await psn.refreshTokens();
} catch (e) {
console.log('error: ', e);
console.log("Trying to login with npsso code");
await psn.auth(npsso);
}
if (psn.access_token == null) {
console.log("Authentication failed. Please check your npsso code if the network to PSN is Ok");
return;
}
console.log(`Authentication Success, You npsso code is consumed in the auth progress and can't be used again. If you want to login in with npsso next time be sure to get a new one`);
try {
let profile = await psn.getProfile("Hakoom");
console.log(profile);
let summary = await psn.getSummary(0, "Hakoom");
console.log(summary);
let individual = await psn.getIndividualGame("NPWR10788_00", "Hakoom");
console.log(individual);
let messages = await psn.getExistingMessageThreads(0);
console.log(messages);
if (messages.threads.length > 0) {
let thread_id = messages.threads[0].threadId;
let message = await psn.getThreadDetail(thread_id, 100);
console.log(message);
}
console.log("psn object holds tokens that are needed for future use , it's best to store them locally before drop the object");
console.log(`Your access_token: ${psn.access_token} //<- You need this to call most APIs`);
console.log(`Your refresh_token: ${psn.refresh_token} //<- You need this to generate new tokens as the access_token only last an hour before it expire.\r\nrefresh_token lasts much long but it would eventually become invalid if you don't regularly call PSN.refreshTokens()`);
console.log("Example finish successfully.");
} catch (e) {
console.log(`error: ${e}`)
console.log(`Something went wrong in one of the API endpoint test. You can try to restart the example with your latest refresh_token: ${psn.refresh_token}`);
}
}
main();