Difference between oauth-shim, oauth-signature and

2019-08-26 14:05发布

问题:

I am new to Node js and still learning on it. I am building a REST API with NodeJS and ExpressJS and having problems setting up OAuth 1.0 with ExpressJS. My requirement is to get some data from an API and post that data. I have the following OAuth 1.0 Authorization information:

  • Consumer Key: 'xxxxx'
  • Consumer Secret: 'xxxx'
  • Token: 'xxxx'
  • Token Secret: 'xxxx'
  • Signature Method: 'HMAC-SHA1'
  • Timestamp:
  • Nonce:
  • Version: 1.0

I came across ouath-request, oauth-shim, oauth-signature and not really sure which one to use correctly.

Also, I have few api urls that I will need data from, e.g.

URL: https://developer.shilpe.com/rest/V1/products/FS01 (GET Method - get product by id FS01)

URL: https://developer.shilpe.com/rest/V1/products (GET Method - get all products)

The following is my code:

const express = require('express');
const bodyParser = require('body-parser');
const crypto = require('crypto');
//const request = require('request');
//const OAuth = require('oauth-1.0a');
const OAuth = require('oauth-request'); //for now im trying with this
//const oauthshim = require('oauth-shim');
//const OAuth = require('oauth-signature');
const config = require('./config'); //I have a config.js file which stores all the keys and tokens

const server = express();

server.use(bodyParser.urlencoded({ extended: true }));
server.use(express.json());

//here i need to use one of the oauth packages, for now im using oauth-request
const shilpe = OAuth({
    consumer: 
    { 
        key: config.MAGENTO.consumerKey, 
        secret: config.MAGENTO.consumerSecret
    },
    signature_method: 'HMAC-SHA1',
    hash_function(base_string, key) 
    {
        return crypto.createHmac('sha1', key).update(base_string).digest('base64');
    }
});

 shilpe.setToken({
     key: config.MAGENTO.tokenKey,
     secret: config.MAGENTO.tokenSecret
 });

//post using expressJS
server.post('/get-shilpe-products', (req, res) => {
   console.log(" -> Server log : get-shilpe-product API request");

//here i will call my url and process with my function
   const URL = 'https://developer.shilpe.com/rest/V1/products/FS01';
   shilpe.get({
      url: URL,
      json: true
   }, function(err, res, products) {
      console.log("result: " + products);
   });
});

//My port
const port = process.env.PORT || 8000;
server.listen(port, () => console.log(`Server is up and running on port ${port}...`));

OUTPUT of this program:

enter image description here

What am I doing wrong? Is there a better way to implement oauth 1.0 authorization with ExpressJS?

POSTMAN OUTPUT of the API url with oauth authorization:

enter image description here