GithubHelp home page GithubHelp logo

Comments (7)

doverradio avatar doverradio commented on May 20, 2024 1

I see the error. I was using .refreshToken() when I should have been using .refreshClientToken().

After making the change, I got a successful newly generated access_token from eBay.

from ebay-api.

dantio avatar dantio commented on May 20, 2024

It's eBay.buy.browse.getItem(), you missed buy.

The token is refreshed automatically if the refresh_token is also set. Instead of storing the access_token you have to store the WHOLE token that looks like this:

{
      access_token: 'accessToken',
      refresh_token_expires_in: 0,
      refresh_token: 'refresh_token', // this is required to refresh the token
      token_type: 'token_type',
      expires_in: 7200
    }

Why are you calling result.json()? The result is already in Json.

Actually you don't even need the Access Token for getItem. Checkout the example here https://hendt.github.io/ebay-api/ maybe it will help you.

from ebay-api.

doverradio avatar doverradio commented on May 20, 2024

Ok and where does the refresh_token get set? During the literal first authorization call?

I will need to know how to do it correctly for other calls. Thank you for guidance on getItem.

from ebay-api.

dantio avatar dantio commented on May 20, 2024

Exactly. const token = await eBay.àuth.oAuth2.getToken(code); will return the whole token and you need to store it.
Than you can use something like

let { token } = await User.findById({_id: user});
eBay.auth.oAuth2.setCredentials(token);

from ebay-api.

doverradio avatar doverradio commented on May 20, 2024

Do you mean like this?

let { token } = await User.findById({_id: user})
let { access_token } = token
eBay.auth.oAuth2.setCredentials(access_token)

from ebay-api.

dantio avatar dantio commented on May 20, 2024

My bad, I fixed my code should be eBay.auth.oAuth2.setCredentials(token);
And the token has the following structure:

{
      access_token: 'accessToken',
      refresh_token_expires_in: 0,
      refresh_token: 'refresh_token', // this is required to refresh the token
      token_type: 'token_type',
      expires_in: 7200
}

from ebay-api.

doverradio avatar doverradio commented on May 20, 2024

I still have been unable to get this right. I'm trying to refresh a User token. After making changes which attempt to trigger refresh token to be generated, it is giving me an error TypeError: Cannot read property 'refreshToken' of undefined.

Here is my code (expressjs controller method):

exports.refreshEbayToken = async (req, res) => {
  let {        
    token // this is the token as we discussed previously (contains access_code, refresh_code, etc.)
    } = req.body

    try {

      eBay.auth.oAuth2.setCredentials(token) // sets the token with expired token

      let post = {
        RequesterCredentials: {
          eBayAuthToken: token[0].access_token 
        },
        Item: {
          Title: `Harry Potter and the Philosopher's Stone`,
          Description: {
            __cdata: `<div>This is the first book in the Harry Potter series. In excellent condition!</div>`
          },
          PrimaryCategory: {
            CategoryID: "377"
          },
          StartPrice: 30,
          CategoryMappingAllowed: true,
          Country: "US",
          Currency: "USD",
          ConditionID: 1000,
          ItemSpecifics: {
            NameValueList: [
              {
                name: "Pages",
                value: "482"
              },
              {
                name: "Ages",
                value: "10-20"
              }
            ]
          },
          DispatchTimeMax: 3,
          ListingDuration: "GTC",
          ListingType: "FixedPriceItem",
          PictureDetails: {
            PictureURL : [
              { PictureURL: `https://mypicserver.com/myphoto.jpg` }
            ]
          },
          PostalCode: "90025",
          Quantity: "1",
          SellerProfiles: {
            SellerShippingProfile: {
              ShippingProfileID: `82379234987`,
              ShippingProfileName: `myShipping`
            },
            SellerReturnProfile: {
              ReturnProfileID: `82379234988`,
              ReturnProfileName: `myReturns`
            },
            SellerPaymentProfile: {
              PaymentProfileID: `82379234989`,
              PaymentProfileName: `myPayments`
            },
          },
        }
      }

      let result = await eBay.trading.AddFixedPriceItem(post) // attempts to post, expected to throw error
    
      console.log(`result: `, result)

    } catch(e) { // due to error, this catch is now triggered...
      console.log(e)
      console.log(`Attempting to refresh token...`);
      
      try {
        let newToken = await eBay.OAuth2.refreshToken()  // Isn't this correct?
        console.log(`newToken: `, newToken) 
        res.json(newToken)

      } catch(error){
        console.log(error)
      }

    }

}

Here is the relevant output I'm getting (I excluded the expected failed initial warning where ebay rejects the listing):

Attempting to refresh token...
TypeError: Cannot read property 'refreshToken' of undefined
    at exports.refreshEbayToken (C:\Users\User\project\backend\controllers\ebay.js:1080:42)
    at process._tickCallback (internal/process/next_tick.js:68:7)
POST /api/ebay/refreshebaytoken - - ms - -

What am I doing wrong? I am trying to list items to multiple accounts and each call requires user access token.

from ebay-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.