Finished eBay API request via UPC
This commit is contained in:
@@ -1,19 +1,72 @@
|
||||
const express = require("express");
|
||||
const cors = require("cors"); // Import the cors middleware
|
||||
const cors = require("cors");
|
||||
const fetch = require("node-fetch"); // Ensure you have 'node-fetch' installed
|
||||
const app = express();
|
||||
|
||||
app.use(cors()); // Use cors middleware to enable CORS
|
||||
app.use(express.json()); // Middleware to parse JSON bodies
|
||||
app.use(cors());
|
||||
app.use(express.json());
|
||||
|
||||
app.post("/submit", (req, res) => {
|
||||
// Set Access-Control-Allow-Origin header explicitly
|
||||
res.setHeader("Access-Control-Allow-Origin", "http://localhost:8000"); // Replace with your client's origin
|
||||
const fetchToken = async () => {
|
||||
const clientID = "TylerPul-ebayimpo-PRD-a983027cf-9b6b8bba"; // Replace with your eBay App ID (Client ID)
|
||||
const clientSecret = "PRD-983027cfae51-634b-42bb-ae58-d68c"; // Replace with your eBay Cert ID (Client Secret)
|
||||
const credentials = Buffer.from(`${clientID}:${clientSecret}`).toString(
|
||||
"base64"
|
||||
);
|
||||
|
||||
// Process the request data here, which is accessible via req.body
|
||||
console.log("Request received durpy durpy:", req.body); // Log the request body
|
||||
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",
|
||||
}
|
||||
);
|
||||
|
||||
// Respond to the client
|
||||
res.status(200).send("Data received");
|
||||
if (!response.ok) throw new Error("Failed to fetch eBay OAuth token");
|
||||
|
||||
const data = await response.json();
|
||||
return data.access_token;
|
||||
} catch (error) {
|
||||
console.error("Error fetching eBay OAuth token:", error);
|
||||
res.status(500).send("Internal Server Error");
|
||||
}
|
||||
};
|
||||
|
||||
app.post("/submit", async (req, res) => {
|
||||
const productCode = req.body.productCode;
|
||||
const oauthToken = await fetchToken();
|
||||
console.log(productCode);
|
||||
try {
|
||||
const response = await fetch(
|
||||
`https://api.ebay.com/buy/browse/v1/item_summary/search?gtin=${productCode}`,
|
||||
{
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer ${oauthToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
}
|
||||
);
|
||||
console.log(response);
|
||||
if (!response.ok)
|
||||
throw new Error("Failed to fetch data from eBay Browse API");
|
||||
|
||||
const data = await response.json();
|
||||
data.itemSummaries.forEach((item) => {
|
||||
console.log(item.price, item.condition);
|
||||
});
|
||||
|
||||
res.status(200).send("Data fetched successfully");
|
||||
} catch (error) {
|
||||
console.error("Error fetching data from eBay Browse API:", error);
|
||||
res.status(500).send("Internal Server Error");
|
||||
}
|
||||
|
||||
// Proceed with your logic here using the token...
|
||||
});
|
||||
|
||||
const PORT = process.env.PORT || 3000;
|
||||
|
||||
Reference in New Issue
Block a user