01-04-2025 05:32 AM
I am working on a Node.js project, and I'm trying to upload products using the Inventory API in the sandbox environment. In the "OAuth Scopes for your app" section, I have the scope https://api.ebay.com/oauth/api_scope/sell.inventory, which is associated with the Authorization Code Grant Type. However, when I run the API, I always receive the following error:
eBay API Error: {
status: 400,
data: {
error: 'invalid_request',
error_description: 'request is missing a required parameter or malformed.'
}
}
Could you please help me identify the issue?
Code:
static async getEbayAccessToken() {
const { EBAY_CLIENT_ID, EBAY_CLIENT_SECRET, EBAY_ENVIRONMENT } = process.env;
const baseURL = EBAY_ENVIRONMENT === 'sandbox'
? 'https://api.sandbox.ebay.com'
: 'https://api.ebay.com';
const tokenUrl = `${baseURL}/identity/v1/oauth2/token`;
const credentials = `${EBAY_CLIENT_ID}:${EBAY_CLIENT_SECRET}`;
const encodedCredentials = Buffer.from(credentials).toString('base64');
try {
const response = await axios.post(
tokenUrl,
'grant_type=authorization_code&scope=https://api.ebay.com/oauth/api_scope/sell.inventory',
{
headers: {
Authorization: `Basic ${encodedCredentials}`,
'Content-Type': 'application/x-www-form-urlencoded',
},
}
);
console.log('Access token retrieved successfully:', response.data);
return response.data.access_token;
} catch (error) {
if (error.response) {
console.error('eBay API Error:', {
status: error.response.status,
data: error.response.data,
});
} else {
console.error('No response from eBay API:', error.message);
}
throw new Error('Failed to fetch eBay access token.');
}
}