delete/create api

This commit is contained in:
2024-03-25 17:00:56 -05:00
parent 5b75f8bb9a
commit 7646ee0fc8
3 changed files with 255 additions and 78 deletions

View File

@@ -4,7 +4,8 @@ import cors from "cors";
import dotenv from "dotenv";
import dataRoutes from "./routes/dataRoutes.js";
import inventoryRoutes from "./routes/inventoryRoutes.js";
import cookieParser from 'cookie-parser';
import cookieParser from "cookie-parser";
import fetchEbayUserToken from "./utils/fetchEbayUserToken.js";
dotenv.config();
@@ -17,6 +18,35 @@ app.use(cookieParser());
app.use("/api/data", dataRoutes);
app.use("/api/inventory", inventoryRoutes);
app.get("/check-inventory", async (req, res) => {
try {
const token = "v^1.1#i^1#r^0#p^3#f^0#I^3#t^H4sIAAAAAAAAAOVZf2wbVx2Pk7QlagOFTYAKGp67wtb07Hd3Pvt8xJac2EncJrUTO1kbQNa7u3fOS+5X794lsQpbSFEloJrGBGjaylYhKMq0aqNUg01bGEhQykQ1hAQCBP2DFXWiCK1sQiD+4M5OXddA29hBscQpUnTvvr8+359+74GlrT17j48c/1uvb1vnqSWw1Onz0dtBz9Ytfe/u6ty1pQPUEfhOLd231L3cdaXfhppqChPINg3dRv5FTdVtobIYDziWLhjQxragQw3ZApGEfHJsVGCCQDAtgxiSoQb8mVQ8EIYyEmUaKuFwTA7zyF3Vr8ssGPFAhI7QihjlWFaMKjwfcb/btoMyuk2gTuIBBjBhCrAUwxUYIHCs+xdkOW464J9Clo0N3SUJgkCiYq5Q4bXqbL21qdC2kUVcIYFEJjmUzyYzqfTBQn+oTlZizQ95Aolj3/w2aMjIPwVVB91ajV2hFvKOJCHbDoQSVQ03CxWS141pwvyKqxVAczFW4qMSK0cUaUM8OWRYGiS3NsNbwTKlVEgFpBNMyrdzqOsMcRZJZO3toCsik/J7/8YdqGIFIyseSA8kD0/m0xMBfz6Xs4x5LCPZA8pwNMcCPsoygYSKjzhYhp4XZKTieWQheU1fVeiasxsUDrrk2GOy/QcNMoBc41Gji8J1LnKJsnrWSirEM6yejqm5kp72QluNp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UhuwQUuh+Xb7JaMoLAKsBCgGRWQqHGEUCoo8TSl8NMoDKEMWtnaut4GHQt2f/c5mnQs1LNQdRv/bdUTo5lvBREfloZd9PwTLvtVOnw/0gz30bnDv1q7J7q4du2xM3M4NlaCNSzokjoWCc6hsQmx13tVx7etfGRnclc5+de/RQvn1J8937Ki7lDz1KfDB2rVkTxe9ve6OEnz4xpct9Hs+0MuEActwDOBYjp0Gu2987abf3333w5cfl3c/+INVWn2q45HLC3NXV3pp0Fsj8vm2dHQv+zqOnV858+3Tr44Xv7Hj/n0vWfTZGHziV6GlE50Xrhx5yzx/z0+PvLFjW9+Vj/3j1T9/fGfm4rHo0/aBv6xc6tbfuhK8FHt05W7qzbOvf/G5CwdOHCXB7y/v+9ojnZ+76+h76W2Dqd/x3w0f+8TQN798cfG1nQ+cGDB/0rv9nYce3zXy5lmwyhz/Jzf88LnPrJx79l3PBM9R33rhpPaFd2Z7Tt/H7Xwy8aMnXjnzvqtPvyRf+/tv+h+dO3nye+G37+l/aGjwNef5PXD17cnTjPyLvV/62TNw1nr5sYuv/Bz9tuS/sLPr09Od0/f/PviU/svFX0Nnn/NCcbXn3u1v/IH/44/PvPji8T9du/TYy3/96OfPnvrI1Q9dNsgn96xWY/kvKQA89C4eAAA="
const limit = req.query.limit || '100'; // Default limit to 10 if not specified
const offset = req.query.offset || '0'; // Default offset to 0 if not specified
const response = await fetch(`https://api.ebay.com/sell/inventory/v1/inventory_item?limit=${limit}&offset=${offset}`, {
method: 'GET',
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${token}`,
"Accept-Language": "en-US", // Explicitly setting the Accept-Language header
},
});
console.log(response)
const inventoryItems = await response.json();
console.log(inventoryItems);
res.json(inventoryItems);
} catch (error) {
console.error('Error checking inventory:', error);
if (!res.headersSent) {
res.status(500).json({ error: 'Internal Server Error' });
}
}
});
/*
the below code needs to be encapsulated somehow. It requires a manual flow, including QUICKLY copy/pasting the code query string from the result of localhost:300/auth/ebay (below)
@@ -32,42 +62,48 @@ app.use("/api/inventory", inventoryRoutes);
2. Add a fetchEbayUserToken endpoint and figure how to cycle user tokens every 2 hours with refresh token
3. Add refresh token to .env and figure out how to safely store user tokens serverside (cookie? knowledge gap here)
*/
app.get('/auth/ebay', async (req, res) => {
// the below URL is hardcoded because it's static in the eBay dev dashboard
const authUrl = `https://auth.ebay.com/oauth2/authorize?client_id=TylerPul-ebayimpo-PRD-a983027cf-9b6b8bba&response_type=code&redirect_uri=Tyler_Pulse-TylerPul-ebayim-ledkmyo&scope=https://api.ebay.com/oauth/api_scope https://api.ebay.com/oauth/api_scope/sell.marketing.readonly https://api.ebay.com/oauth/api_scope/sell.marketing https://api.ebay.com/oauth/api_scope/sell.inventory.readonly https://api.ebay.com/oauth/api_scope/sell.inventory https://api.ebay.com/oauth/api_scope/sell.account.readonly https://api.ebay.com/oauth/api_scope/sell.account https://api.ebay.com/oauth/api_scope/sell.fulfillment.readonly https://api.ebay.com/oauth/api_scope/sell.fulfillment https://api.ebay.com/oauth/api_scope/sell.analytics.readonly https://api.ebay.com/oauth/api_scope/sell.finances https://api.ebay.com/oauth/api_scope/sell.payment.dispute https://api.ebay.com/oauth/api_scope/commerce.identity.readonly https://api.ebay.com/oauth/api_scope/sell.reputation https://api.ebay.com/oauth/api_scope/sell.reputation.readonly https://api.ebay.com/oauth/api_scope/commerce.notification.subscription https://api.ebay.com/oauth/api_scope/commerce.notification.subscription.readonly https://api.ebay.com/oauth/api_scope/sell.stores https://api.ebay.com/oauth/api_scope/sell.stores.readonly`;
res.redirect(authUrl); // Redirect the user to eBay's sign-in page
});
// Step 2: Handle the redirect from eBay
app.get('/auth/ebay/callback', async (req, res) => {
// this code comes from a query string at localhost:3000/auth/ebay when we redirect
const code = 'v%5E1.1%23i%5E1%23p%5E3%23f%5E0%23r%5E1%23I%5E3%23t%5EUl41Xzc6NUE2ODc0NkU5Q0Q4N0QxQjhENTVCNzAxQTAwMEM2MzlfMF8xI0VeMjYw'
try {
// Step 3: Exchange the authorization code for access and refresh tokens
const tokenResponse = await fetch('https://api.ebay.com/identity/v1/oauth2/token', {
method: 'POST',
app.get("/auth/ebay", async (req, res) => {
// the below URL is hardcoded because it's static in the eBay dev dashboard
const authUrl = `https://auth.ebay.com/oauth2/authorize?client_id=TylerPul-ebayimpo-PRD-a983027cf-9b6b8bba&response_type=code&redirect_uri=Tyler_Pulse-TylerPul-ebayim-ledkmyo&scope=https://api.ebay.com/oauth/api_scope https://api.ebay.com/oauth/api_scope/sell.marketing.readonly https://api.ebay.com/oauth/api_scope/sell.marketing https://api.ebay.com/oauth/api_scope/sell.inventory.readonly https://api.ebay.com/oauth/api_scope/sell.inventory https://api.ebay.com/oauth/api_scope/sell.account.readonly https://api.ebay.com/oauth/api_scope/sell.account https://api.ebay.com/oauth/api_scope/sell.fulfillment.readonly https://api.ebay.com/oauth/api_scope/sell.fulfillment https://api.ebay.com/oauth/api_scope/sell.analytics.readonly https://api.ebay.com/oauth/api_scope/sell.finances https://api.ebay.com/oauth/api_scope/sell.payment.dispute https://api.ebay.com/oauth/api_scope/commerce.identity.readonly https://api.ebay.com/oauth/api_scope/sell.reputation https://api.ebay.com/oauth/api_scope/sell.reputation.readonly https://api.ebay.com/oauth/api_scope/commerce.notification.subscription https://api.ebay.com/oauth/api_scope/commerce.notification.subscription.readonly https://api.ebay.com/oauth/api_scope/sell.stores https://api.ebay.com/oauth/api_scope/sell.stores.readonly`;
res.redirect(authUrl); // Redirect the user to eBay's sign-in page
});
// Step 2: Handle the redirect from eBay
app.get("/auth/ebay/callback", async (req, res) => {
// this code comes from a query string at localhost:3000/auth/ebay when we redirect
const code =
"v%5E1.1%23i%5E1%23p%5E3%23f%5E0%23r%5E1%23I%5E3%23t%5EUl41Xzc6NUE2ODc0NkU5Q0Q4N0QxQjhENTVCNzAxQTAwMEM2MzlfMF8xI0VeMjYw";
try {
// Step 3: Exchange the authorization code for access and refresh tokens
const tokenResponse = await fetch(
"https://api.ebay.com/identity/v1/oauth2/token",
{
method: "POST",
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': `Basic ${Buffer.from(`${process.env.EBAY_CLIENT_ID}:${process.env.EBAY_CLIENT_SECRET}`).toString('base64')}`
"Content-Type": "application/x-www-form-urlencoded",
Authorization: `Basic ${Buffer.from(
`${process.env.EBAY_CLIENT_ID}:${process.env.EBAY_CLIENT_SECRET}`
).toString("base64")}`,
},
// we use Tyler_Pulse-TylerPul-ebayim-ledkmyo and not the initial redirect URL
body: `grant_type=authorization_code&code=${code}&redirect_uri=Tyler_Pulse-TylerPul-ebayim-ledkmyo`
});
if (!tokenResponse.ok) {
throw new Error('Failed to exchange authorization code for tokens');
body: `grant_type=authorization_code&code=${code}&redirect_uri=Tyler_Pulse-TylerPul-ebayim-ledkmyo`,
}
const tokenData = await tokenResponse.json();
console.log('Access Token:', tokenData.access_token);
console.log('Refresh Token:', tokenData.refresh_token);
res.send('Authentication successful! Tokens acquired.'); // For demonstration purposes; you might want to redirect the user or show a different message
} catch (error) {
console.error('Error during token exchange:', error);
res.status(500).send('Internal Server Error');
);
if (!tokenResponse.ok) {
throw new Error("Failed to exchange authorization code for tokens");
}
});
const tokenData = await tokenResponse.json();
console.log("Access Token:", tokenData.access_token);
console.log("Refresh Token:", tokenData.refresh_token);
res.send("Authentication successful! Tokens acquired."); // For demonstration purposes; you might want to redirect the user or show a different message
} catch (error) {
console.error("Error during token exchange:", error);
res.status(500).send("Internal Server Error");
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));