Finished refresh/user token auth flow

This commit is contained in:
2024-03-23 23:47:46 -05:00
parent 6dddc5799f
commit 461b311347
7 changed files with 110 additions and 146 deletions

View File

@@ -1,41 +1,7 @@
import fetch from "node-fetch";
const fetchEbayUserToken = async (authorizationCode) => {
const clientId = process.env.EBAY_CLIENT_ID;
const clientSecret = process.env.EBAY_CLIENT_SECRET;
const redirectUri = process.env.EBAY_REDIRECT_URI; // Make sure this matches the URI registered with eBay
const credentials = Buffer.from(`${clientId}:${clientSecret}`).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=authorization_code&code=${authorizationCode}&redirect_uri=${encodeURIComponent(
redirectUri
)}`,
}
);
if (!response.ok) {
const errorBody = await response.text();
throw new Error(
`Failed to fetch eBay user token: ${response.status} ${response.statusText} - ${errorBody}`
);
}
const data = await response.json();
return data.access_token; // This is the User access token
} catch (error) {
console.error("Error fetching eBay user token:", error);
throw error;
}
// this is where we will use our refresh token and cycle our user tokens
};
export default fetchEbayUserToken;