psn-api
Version:
A well-tested library that lets you get trophy, user, and game data from the PlayStation Network.
47 lines (40 loc) • 1.45 kB
text/typescript
import fetch from "isomorphic-unfetch";
import type { AuthTokensResponse } from "../models";
import { AUTH_BASE_URL } from "./AUTH_BASE_URL";
/**
* @param accessCode Your access code, typically retrieved by using `exchangeNpssoForAccessCode()`.
* @returns An object containing an access token, refresh token, and expiry times for both.
*/
export const exchangeAccessCodeForAuthTokens = async (
accessCode: string
): Promise<AuthTokensResponse> => {
const requestUrl = `${AUTH_BASE_URL}/token`;
const res = await fetch(requestUrl, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Authorization:
"Basic MDk1MTUxNTktNzIzNy00MzcwLTliNDAtMzgwNmU2N2MwODkxOnVjUGprYTV0bnRCMktxc1A="
},
body: new URLSearchParams({
code: accessCode,
redirect_uri: "com.scee.psxandroid.scecompcall://redirect",
grant_type: "authorization_code",
token_format: "jwt"
}).toString()
});
const raw = await res.json();
return {
accessToken: raw.access_token,
expiresIn: raw.expires_in,
idToken: raw.id_token,
refreshToken: raw.refresh_token,
refreshTokenExpiresIn: raw.refresh_token_expires_in,
scope: raw.scope,
tokenType: raw.token_type
};
};
/**
* @deprecated Use `exchangeAccessCodeForAuthTokens` instead. This alias will be removed in a future version.
*/
export const exchangeCodeForAccessToken = exchangeAccessCodeForAuthTokens;