'NodeJS hendt/ebay-api / getOrders / Error - Insufficient permissions to fulfill the request

So I am using the hendt/ebay-api library but having no success with the getOrders call.

Here's the auth scopes I'm using when setting the ORIGINAL User access token...:

      eBay.auth.oAuth2.setScope([
        'https://api.ebay.com/oauth/api_scope',
        'https://api.ebay.com/oauth/api_scope/sell.marketing.readonly',
        'https://api.ebay.com/oauth/api_scope/sell.marketing',
        'https://api.ebay.com/oauth/api_scope/sell.inventory.readonly',
        'https://api.ebay.com/oauth/api_scope/sell.inventory',
        'https://api.ebay.com/oauth/api_scope/sell.account',
        'https://api.ebay.com/oauth/api_scope/sell.account.readonly',
        'https://api.ebay.com/oauth/api_scope/sell.fulfillment.readonly',
        'https://api.ebay.com/oauth/api_scope/sell.fulfillment',
        'https://api.ebay.com/oauth/api_scope/sell.analytics.readonly',
        'https://api.ebay.com/oauth/api_scope/sell.finances',
        'https://api.ebay.com/oauth/api_scope/sell.payment.dispute',
        'https://api.ebay.com/oauth/api_scope/commerce.identity.readonly',
    ]);

Here's my code (ExpressJS backend controller method...):

exports.getOrders = async (req, res) => { 
  let  _id = req.params.userId; // user's _id
  let token = await refreshEbayToken(_id, eBay)
  console.log(`token...`, token) // not an array!  It's an object!

  // set OAuth2 eBay credentials
  eBay.auth.oAuth2.setCredentials(token.access_token);

  try {
    let result = await eBay.sell.fulfillment.getOrders({
      filter: 'orderfulfillmentstatus:{NOT_STARTED|IN_PROGRESS}',
        limit: 5
    })
    console.log(`Got a result...`, result)
    res.json(result)
  } catch (e) {
    console.log(`Got an error in getOrders...`, e)
    res.status(400).json( { error: e } )
  }
  
}

Here's the error I get:

{
        "meta": {
            "errors": [
                {
                    "errorId": 1100,
                    "domain": "ACCESS",
                    "category": "REQUEST",
                    "message": "Access denied",
                    "longMessage": "Insufficient permissions to fulfill the request."
                }
            ]
        },
        "name": "EBayAccessDenied"
    }

Even freshly minted User access tokens through those scopes get this error.

According to the ebay docs on getOrders, these are the required auth scopes for this call:

https://api.ebay.com/oauth/api_scope/sell.fulfillment
https://api.ebay.com/oauth/api_scope/sell.fulfillment.readonly

Upon further investigation, I found I am certainly using those scopes in the response (below is a snippet of the scopes grabbed from the larger ebay error)...

...%20https%3A%2F%2Fapi.ebay.com%2Foauth%2Fapi_scope%2Fsell.fulfillment.readonly%20https%3A%2F%2Fapi.ebay.com%2Foauth%2Fapi_scope%2Fsell.fulfillment%20https%3A%2F%2Fapi.ebay.com%2Foauth%2Fapi_scope%2Fsell.analytics.readonly%20https%3A%2F%2Fapi.ebay.com%2Foauth%2Fapi_scope%2Fsell.finances%20...

As shown above, I am clearly using the sell.fulfillment and sell.fulfillment.readonly scopes so I do not understand what is the issue.

What do you think could be the issue?



Solution 1:[1]

I finally got it working.

Here is an example of what I receive now:

{
    "sales": [
        {
            "orderId": "xxxxxxxxx",
            "legacyOrderId": "xxxxxxxxx-xxxxxxxxx",
            "creationDate": "2022-03-09T23:10:36.000Z",
            "lastModifiedDate": "2022-03-18T15:51:04.000Z",
            "orderFulfillmentStatus": "FULFILLED",
            "orderPaymentStatus": "PAID",
            "sellerId": "xxxx",
            "buyer": {
                "username": "xxxxx",
                "taxAddress": {
                    "stateOrProvince": "xx",
                    "postalCode": "xxxxxxxx",
                    "countryCode": "xx"
                }
            },
            "pricingSummary": {
                "priceSubtotal": {
                    "value": "xxxxx",
                    "currency": "USD"
                },
                "deliveryCost": {
                    "value": "0.0",
                    "currency": "USD"
                },
                "total": {
                    "value": "xxxxx",
                    "currency": "USD"
                }
            },
            "cancelStatus": {
                "cancelState": "NONE_REQUESTED",
                "cancelRequests": []
            },
            "paymentSummary": {
                "totalDueSeller": {
                    "value": "xxxxx",
                    "currency": "USD"
                },
                "refunds": [],
                "payments": [
                    {
                        "paymentMethod": "EBAY",
                        "paymentReferenceId": "xxxxxx",
                        "paymentDate": "2022-03-09T23:15:33.109Z",
                        "amount": {
                            "value": "xxxxxxxx",
                            "currency": "USD"
                        },
                        "paymentStatus": "PAID"
                    }
                ]
            },
            "fulfillmentStartInstructions": [
                {
                    "fulfillmentInstructionsType": "SHIP_TO",
                    "minEstimatedDeliveryDate": "2022-03-14T07:00:00.000Z",
                    "maxEstimatedDeliveryDate": "2022-03-19T07:00:00.000Z",
                    "ebaySupportedFulfillment": false,
                    "shippingStep": {
                        "shipTo": {
                            "fullName": "xxxxxxxxxx",
                            "contactAddress": {
                                "addressLine1": "xxxxxxxxxxx",
                                "city": "xxxxxxxxxx",
                                "stateOrProvince": "xx",
                                "postalCode": "xxxxxxxxx",
                                "countryCode": "US"
                            },
                            "primaryPhone": {
                                "phoneNumber": "xxxxxxxxxxxxx"
                            },
                            "email": "[email protected]"
                        },
                        "shippingServiceCode": "ShippingMethodStandard"
                    }
                }
            ],
            "fulfillmentHrefs": [
                "https://api.ebay.com/sell/fulfillment/v1/order/xxxxxxxxxxx/shipping_fulfillment/xxxxxxxxxx"
            ],
            "lineItems": [
                {
                    "lineItemId": "xxxxxxxxxx",
                    "legacyItemId": "xxxxxxxxxx",
                    "legacyVariationId": "xxxxxxxxxx",
                    "sku": "xxxxxxxxxx",
                    "title": "xxxxxxxxxx",
                    "lineItemCost": {
                        "value": "xxxxxxxxxx",
                        "currency": "USD"
                    },
                    "quantity": 1,
                    "soldFormat": "FIXED_PRICE",
                    "listingMarketplaceId": "EBAY_US",
                    "purchaseMarketplaceId": "EBAY_US",
                    "lineItemFulfillmentStatus": "FULFILLED",
                    "total": {
                        "value": "xxxxxxxxxx",
                        "currency": "USD"
                    },
                    "deliveryCost": {
                        "shippingCost": {
                            "value": "0.0",
                            "currency": "USD"
                        }
                    },
                    "appliedPromotions": [],
                    "taxes": [],
                    "ebayCollectAndRemitTaxes": [
                        {
                            "taxType": "STATE_SALES_TAX",
                            "amount": {
                                "value": "xxxxxxxxxx",
                                "currency": "USD"
                            },
                            "collectionMethod": "NET"
                        }
                    ],
                    "properties": {
                        "buyerProtection": true
                    },
                    "lineItemFulfillmentInstructions": {
                        "minEstimatedDeliveryDate": "2022-03-14T07:00:00.000Z",
                        "maxEstimatedDeliveryDate": "2022-03-19T07:00:00.000Z",
                        "shipByDate": "2022-03-14T06:59:59.000Z",
                        "guaranteedDelivery": false
                    },
                    "itemLocation": {
                        "location": "xxxxxxxxxx",
                        "countryCode": "US",
                        "postalCode": "xxxxxxxxxx"
                    }
                }
            ],
            "ebayCollectAndRemitTax": true,
            "salesRecordReference": "xxxxxxxxxx",
            "totalFeeBasisAmount": {
                "value": "xxxxxxxxxx",
                "currency": "USD"
            },
            "totalMarketplaceFee": {
                "value": "xxxxxxxxxx",
                "currency": "USD"
            }
        }
    ]
}

Here are my scopes:

      eBay.auth.oAuth2.setScope([
        'https://api.ebay.com/oauth/api_scope',
        'https://api.ebay.com/oauth/api_scope/sell.marketing.readonly',
        'https://api.ebay.com/oauth/api_scope/sell.marketing',
        'https://api.ebay.com/oauth/api_scope/sell.inventory.readonly',
        'https://api.ebay.com/oauth/api_scope/sell.inventory',
        'https://api.ebay.com/oauth/api_scope/sell.account',
        'https://api.ebay.com/oauth/api_scope/sell.account.readonly',
        'https://api.ebay.com/oauth/api_scope/sell.fulfillment.readonly',
        'https://api.ebay.com/oauth/api_scope/sell.fulfillment',
        'https://api.ebay.com/oauth/api_scope/sell.analytics.readonly',
        'https://api.ebay.com/oauth/api_scope/sell.finances',
        'https://api.ebay.com/oauth/api_scope/sell.payment.dispute',
        'https://api.ebay.com/oauth/api_scope/commerce.identity.readonly',
    ]);

Here is the code getting it right ( express.js )...

    exports.getOrders = async (req, res) => {  // https://developer.ebay.com/Devzone/XML/docs/Reference/eBay/GetOrders.html

      let  _id = req.params.userId; // user's _id
      let a = {}
      a._id = _id
      a.userDoc = await User.findById( { _id: a._id } )
      eBay.auth.oAuth2.setCredentials( a.userDoc.token[ 0 ] ) // set OAuth2 eBay credentials
      try 
      {
        a.result = await eBay.trading.getOrders({
          filter: 'orderfulfillmentstatus:{NOT_STARTED|IN_PROGRESS}',
            limit: 5
        })
        console.log(`Got a result...`, a.result)
        } catch ( e ) {
      }
    }

I send in the mongodb user _id as request params and use that to pull out their user token, which is passed into eBay.auth.oAuth2.setCredentials( a.userDoc.token[ 0 ] ) and allows the call to actually be made... eBay.trading.getOrders

I hope this helps someone out there. I remember being unable to make this call for months.

Solution 2:[2]

So I am using the library but having no success with the getOrders call.

Here's the auth scopes I'm using when setting the ORIGINAL User access token

https://gist.github.com/mudassaralichouhan/d952b464b2907c7f833c87c05152eb76

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rom
Solution 2 Mudassar Ali Chouhan