// Need to figoure out expiration and make sure to cycle this appropriately to avoid unnecessary calls import fetch from "node-fetch"; import { LoggingLevel, smartLogging } from "./helper.js"; const fetchEbayReadToken = async () => { smartLogging(LoggingLevel.AppTrace, `fetchEbayReadToken`); const ebayClientId = process.env.EBAY_CLIENT_ID; const ebayClientSecret = process.env.EBAY_CLIENT_SECRET; const credentials = Buffer.from( `${ebayClientId}:${ebayClientSecret}` ).toString("base64"); try { smartLogging(LoggingLevel.Debug, `Fetching Token from eBay`); const response = await fetch( "https://api.ebay.com/identity/v1/oauth2/token", { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded", Authorization: `Basic ${credentials}`, }, body: "grant_type=client_credentials&scope=https%3A%2F%2Fapi.ebay.com%2Foauth%2Fapi_scope", } ); if (!response.ok) { const errorBody = await response.text(); throw new Error( `Failed to fetch eBay OAuth token: ${response.status} ${response.statusText} - ${errorBody}` ); } const data = await response.json(); return data.access_token; } catch (error) { smartLogging(LoggingLevel.Error, `Error fetching eBay OAuth token: ${error}`); throw error; // Throw the error to be handled by the caller } }; export default fetchEbayReadToken;