drbarzaga / walmart-mws

Walmart Marketplace SDK is a wrapper sdk around the Walmart Marketplace API to used this sdk you will need an developer account with the ClientId and ClientSecret keys.
MIT License
8 stars 4 forks source link

ECONNREFUSED #16

Closed doverradio closed 2 years ago

doverradio commented 2 years ago

My code (portions are taken from your example):

    let a = {}
    try {
        a.data = {
            sku:'192503120522',
            quantity:{
                unit: 'EACH', 
                amount: 0
            }
        }
        a.result = await walmartMws.inventory.updateItemInventory( '192503120522', a.data )
        res.json( a.result )
    } catch ( e ) {
        log( `updateItemInventory e: `, e )
        res.status( 400 ).json( { error: e } )
    }

So, after running that, I got the following error...

{
    "error": {
        "message": "connect ECONNREFUSED 127.0.0.1:80",
        "name": "Error",
        "stack": "Error: connect ECONNREFUSED 127.0.0.1:80\n    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16)",
        "config": {
            "url": "VERSION/token",
            "method": "post",
            "data": "grant_type=client_credentials",
            "headers": {
                "Accept": "application/json",
                "Content-Type": "application/x-www-form-urlencoded",
                "WM_SVC.NAME": "NAME",
                "WM_QOS.CORRELATION_ID": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
                "Authorization": "Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
                "User-Agent": "axios/0.19.2",
                "Content-Length": 29
            },
            "baseURL": "HOST",
            "transformRequest": [
                null
            ],
            "transformResponse": [
                null
            ],
            "timeout": 0,
            "xsrfCookieName": "XSRF-TOKEN",
            "xsrfHeaderName": "X-XSRF-TOKEN",
            "maxContentLength": -1
        },
        "code": "ECONNREFUSED"
    }
}

What am I doing wrong?

drbarzaga commented 2 years ago

@doverradio Hi are you passing these values https://github.com/drbarzaga/walmart-mws/blob/master/src/walmart-mws.js#L5 ? I think the problem is a config problem with your host. You need to use the Walmart API URL as the host.

doverradio commented 2 years ago

Ah I see I failed to put any value to replace HOST.

What is the Walmart API URL? Can you please share it or where I can locate it?

drbarzaga commented 2 years ago

@doverradio When you create a Developer Account in Walmart you have access to this URL in your dashboard.

doverradio commented 2 years ago

Thank you for that, I was just in the "Try it now" API explorer and found this https://marketplace.walmartapis.com.

I immediately tested this as the base url so my config is now;

var walmartMws = require("../node_modules/walmart-mws/src/walmart-msw")(
    'xxxxxxxCLIENTIDxxxxxx',
    'xxxxCLIENTSECRETxxxxxxxxxxxxxxxxxxxxxxxxxx', 
    'https://marketplace.walmartapis.com/', 
    'VERSION', 
    'NAME'
);

The result is I am still getting error:

{
    "error": {
        "message": "Request failed with status code 400",
        "name": "Error",
        "stack": "Error: Request failed with status code 400\n    at createError (C:\\Users\\project\\backend\\node_modules\\walmart-mws\\node_modules\\axios\\lib\\core\\createError.js:16:15)\n    at settle (C:\\Users\\project\\backend\\node_modules\\walmart-mws\\node_modules\\axios\\lib\\core\\settle.js:17:12)\n    at IncomingMessage.handleStreamEnd (C:\\Users\\project\\backend\\node_modules\\walmart-mws\\node_modules\\axios\\lib\\adapters\\http.js:236:11)\n    at IncomingMessage.emit (node:events:402:35)\n    at IncomingMessage.emit (node:domain:475:12)\n    at endReadableNT (node:internal/streams/readable:1343:12)\n    at processTicksAndRejections (node:internal/process/task_queues:83:21)",
        "config": {
            "url": "VERSION/token",
            "method": "post",
            "data": "grant_type=client_credentials",
            "headers": {
                "Accept": "application/json",
                "Content-Type": "application/x-www-form-urlencoded",
                "WM_SVC.NAME": "NAME",
                "WM_QOS.CORRELATION_ID": "xxxxxxxxxxxxxxxxxxxxxxxxxx",
                "Authorization": "Basic xxxxxxxxxxxxxxxxxxxxxxxxxx",
                "User-Agent": "axios/0.19.2",
                "Content-Length": 29
            },
            "baseURL": "https://marketplace.walmartapis.com/",
            "transformRequest": [
                null
            ],
            "transformResponse": [
                null
            ],
            "timeout": 0,
            "xsrfCookieName": "XSRF-TOKEN",
            "xsrfHeaderName": "X-XSRF-TOKEN",
            "maxContentLength": -1
        }
    }
}

I feel really close to solving this but still not yet there. I did not put any value for VERSION. Could this be the cause of the error?

drbarzaga commented 2 years ago

@doverradio The problem is that you are using the wrong URL https://marketplace.walmartapis.com/ the host is another no this.

doverradio commented 2 years ago

Ok, I'm totally lost then.

Upon viewing the Walmart Developer Portal items guide, I find they give an example endpoint like so: https://marketplace.walmartapis.com/v3/feeds?feedType=MP_ITEM_MATCH

Their direct Bulk Item Setup (Multiple) instructions are not providing any url as well.

I am continuing to search further for the correct url but, if you know it, please can you share it? I am willing to try anything at this point because I have around 340,000 items I am trying to post.

drbarzaga commented 2 years ago

@doverradio Ok no problem.

doverradio commented 2 years ago

Please see screenshot:

It really does seem they are pushing that url on me: https://marketplace.walmartapis.com/

image

doverradio commented 2 years ago

Solved it!

Here's my final code which generated a valid access_token:

// using express.js, this is a single controller method...
exports.walmartApiGetToken = async ( req, res ) => {
    try {
        let endpoint = `https://marketplace.walmartapis.com/v3/token`
        let headers = { 
            Accept: "application/json", 
            "Content-Type": "application/x-www-form-urlencoded",
            Authorization: `Basic ${Buffer.from(
                `${ CLIENT_ID` }:${ `CLIENT_SECRET` }`
                ).toString('base64')}`,
            "WM_QOS.CORRELATION_ID": uuidv4(),
            "WM_SVC.NAME": "Walmart Marketplace",
        }
        let method = `POST`
        let body = new URLSearchParams();
        body.append('grant_type', 'client_credentials');
        let result = await fetch( endpoint, { method, headers, body } )
        result = await result.json()
        console.log( `result...`, result )
        res.json(result)
    } catch ( e ) { console.log( `Got an error in walmartApiGetToken...`, e ); res.json( { error: e } ) }
}

Got the result...

{
    "access_token": "XXXXXXXXXXXXXXXXSUPERLONGACCESSTOKENXXXXXXXXXXXXXXXXXXXXXX",
    "token_type": "Bearer",
    "expires_in": 900
}
drbarzaga commented 2 years ago

@doverradio Cool!