Added refresh user token flow using httpOnly cookies
This commit is contained in:
40
api/utils/fetchEbayApplicationToken.js
Normal file
40
api/utils/fetchEbayApplicationToken.js
Normal 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;
|
||||
Reference in New Issue
Block a user