Next.js Discord

Discord Forum

Error Obtaining eBay OAuth Access Token on Token Exchange Request

Unanswered
Philippine Crocodile posted this in #help-forum
Open in Discord
Philippine CrocodileOP
I'm working on integrating eBay OAuth into my application for user authentication. I've successfully implemented the first part of the OAuth flow, where the user authorizes my application, and I receive an authorization code from eBay. However, I'm encountering an issue when attempting to exchange this authorization code for an access token using the eBay API.

Here's the process I've followed:

1. Redirected the user to eBay for authorization and received an authorization code.
2. Attempted to exchange the authorization code for an access token.

When making the POST request to https://api.sandbox.ebay.com/identity/v1/oauth2/token (using the sandbox environment for testing), I encounter the following error:

{
"error": "401 unauthorized"
}

Callback.js:
import axios from 'axios';
import qs from 'qs';

export default async function handler(req, res) {
  if (req.method === 'GET') {
    try {
      const code = req.query.code;

      const credentials = Buffer.from(`thisurl`).toString('base64');
      const authorizationHeader = `Basic ${credentials}`;

      const response = await axios({
        url: "https://api.sandbox.ebay.com/identity/v1/oauth2/token",
        method: "post",
        headers: {
          "Content-Type": "application/x-www-form-urlencoded",
          Authorization: authorizationHeader,
        },
        data: qs.stringify({
          grant_type: "authorization_code",
          code: code,
          redirect_uri: "URI",
        })
      });

      res.status(200).json(response.data); // Return the token data
    } catch (err) {
      console.error(err);
      res.status(500).json({ error: 'Error exchanging code for token' });
    }
  } else {
    res.status(405).json({ error: 'Method not allowed' });
  }
}


The second time around, auth is getting access token from code, and I don't understand why. Any help or resources would be appreciated!

0 Replies