Added refresh user token flow using httpOnly cookies

This commit is contained in:
2024-03-25 09:50:37 -05:00
parent 461b311347
commit 5b75f8bb9a
18 changed files with 1249 additions and 9 deletions

View File

@@ -0,0 +1,40 @@
// Need to figoure out expiration and make sure to cycle this appropriately to avoid unnecessary calls
import fetch from "node-fetch";
const fetchEbayApplicationToken = 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 fetchEbayApplicationToken;