Add a command to get a session token

This commit is contained in:
Samuel Elliott
2022-03-16 20:14:33 +00:00
parent 5610cf8630
commit 5aba992899
4 changed files with 145 additions and 19 deletions

View File

@@ -4,6 +4,35 @@ import { ErrorResponse } from './util.js';
const debug = createDebug('api:na');
export async function getNintendoAccountSessionToken(code: string, verifier: string, client_id: string) {
debug('Getting Nintendo Account session token');
const response = await fetch('https://accounts.nintendo.com/connect/1.0.0/api/session_token', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'X-Platform': 'Android',
'X-ProductVersion': '2.0.0',
'User-Agent': 'NASDKAPI; Android',
},
body: new URLSearchParams({
client_id,
session_token_code: code,
session_token_code_verifier: verifier,
}).toString(),
});
const token = await response.json() as NintendoAccountSessionToken | NintendoAccountError;
if ('errorCode' in token) {
throw new ErrorResponse('[na] + ' + token.detail, response, token);
}
debug('Got Nintendo Account session token', token);
return token;
}
export async function getNintendoAccountToken(token: string) {
debug('Getting Nintendo Account token');
@@ -56,6 +85,11 @@ export async function getNintendoAccountUser(token: NintendoAccountToken) {
return user;
}
export interface NintendoAccountSessionToken {
session_token: string;
code: string;
}
export interface NintendoAccountToken {
scope: ['openid', 'user', 'user.birthday', 'user.mii', 'user.screenName'];
token_type: 'Bearer';