-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs
More file actions
57 lines (47 loc) · 1.45 KB
/
Copy pathjs
File metadata and controls
57 lines (47 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
require('dotenv').config();
const express = require('express');
const axios = require('axios');
const cors = require('cors');
const app = express();
app.use(cors());
const PORT = 3000;
async function getToken() {
const auth = Buffer.from(`${process.env.EBAY_CLIENT_ID}:${process.env.EBAY_CLIENT_SECRET}`).toString('base64');
const res = await axios.post('https://api.sandbox.ebay.com/identity/v1/oauth2/token',
new URLSearchParams({
grant_type: 'client_credentials',
scope: 'https://api.ebay.com/oauth/api_scope'
}), {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': `Basic ${auth}`
}
});
return res.data.access_token;
}
app.get('/buscar', async (req, res) => {
try {
const query = req.query.q || 'reloj';
const token = await getToken();
const response = await axios.get('https://api.sandbox.ebay.com/buy/browse/v1/item_summary/search', {
headers: {
Authorization: `Bearer ${token}`
},
params: {
q: query,
limit: 5,
deliveryCountry: 'PY'
}
});
const items = response.data.itemSummaries.map(i => ({
titulo: i.title,
precio: i.price?.value + ' ' + i.price?.currency,
link: i.itemWebUrl
}));
res.json(items);
} catch (err) {
console.error(err.message);
res.status(500).send('Error en la búsqueda');
}
});
app.listen(PORT, () => console.log(`Servidor en http://localhost:${PORT}`));