Files
ebay_api/api/utils/fetchEbayReadToken.js

40 lines
1.2 KiB
JavaScript

// Need to figoure out expiration and make sure to cycle this appropriately to avoid unnecessary calls
import fetch from "node-fetch";
const fetchEbayReadToken = async () => {
const ebayClientId = process.env.EBAY_CLIENT_ID;
const ebayClientSecret = process.env.EBAY_CLIENT_SECRET;
const credentials = Buffer.from(
`${ebayClientId}:${ebayClientSecret}`
).toString("base64");
try {
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) {
console.error("Error fetching eBay OAuth token:", error);
throw error; // Throw the error to be handled by the caller
}
};
export default fetchEbayReadToken;