Muhammad Zaid Ikhlas
Muhammad Zaid Ikhlas

Reputation: 109

Can't generate signature for Lazada's API in NodeJS

I am trying to consume Lazada API through HTTP requests in NodeJS and it requires a signature as one of the parameters. To generate that, I'm using the js-256 package but for some reason I'm getting an IncompleSignature error.

The exact error message:

{ type: 'ISV',
  code: 'IncompleteSignature',
  message: 'The request signature does not conform to lazada standards',
  request_id: '0be6e79215428302067761224' }

My code:

var sha256 = require('js-sha256');


module.exports = function(app, Request){

    app.get('/', function(req,res){

        var access_token = "myToken";
        var app_key = "myAppKey";
        var order_id = "36835322434";
        var sign_method = "sha256";
        var timestamp = new Date().timestamp;

        var concatenatedString = "/order/items/getaccess_token"+access_token
        +"app_key"+app_key
        +"order_id"+order_id
        +"sign_method"+sign_method
        +"timestamp"+new Date().getTime();


        var hash = sha256.hmac.create("myAppSecret");
        hash.update(concatenatedString);

        var httpRequestLink = "http://api.lazada.co.th/rest/order/items/get?access_token="+access_token
        +"&app_key="+app_key
        +"&order_id="+order_id
        +"&sign_method="+sign_method
        +"&timestamp="+new Date().getTime()
        +"&sign="+hash.hex();

        Request.get(httpRequestLink, (error, response, body) => {
            if(error) {
                return console.log(error);
            }
            console.log(JSON.parse(body));
        }); 

    });

}

Would really appreciate if someone can help me out here. Thanks

Upvotes: 0

Views: 3674

Answers (3)

Zaheer Bashir
Zaheer Bashir

Reputation: 108

how can we get the access token using this package because I can't under where I will get the AuthCode

const authCode = '123' // replace valid authCode here const params = { code: authCode, } const response = aLazadaAPI .generateAccessToken(params) .then(response => console.log(JSON.stringify(response, null, 4))) .catch(error => console.log(JSON.stringify(error, null, 4)))

Upvotes: 0

May Noppadol
May Noppadol

Reputation: 648

My code work perfectly with follow generate sign by this package.

const CryptoJS = require("crypto-js");
const config = require('config')
const LAZADA_SECRET_KEY = config.get("LAZADA_SECRET_KEY")


exports.encryptMessage = (path) =>
    new Promise((resolve, reject) => {
    const encryptMessage = CryptoJS.HmacSHA256(path, LAZADA_SECRET_KEY).toString(
        CryptoJS.enc.Hex,
    );

    let sign = encryptMessage.toUpperCase();

    return resolve(sign);
});

Upvotes: 1

Sourabh Bhutani
Sourabh Bhutani

Reputation: 663

I'll suggest to add npm package lazada-open-platform-sdk url https://www.npmjs.com/package/lazada-open-platform-sdk.

You can easily call lazada api's by its functions and mostly all are integrated by them.For finanace and seller apis which are not integrated by package for that you can use access_token generated by generateAccessToken and call apis.

Upvotes: 0

Related Questions