Generate oAuth nonce for parallel requests

后端 未结 3 1150
生来不讨喜
生来不讨喜 2021-01-21 09:12

I am requesting Bitstamp API in parrallel:

// Simplified version

var async = require(\'async\');
var bitstamp = require(\'bitstamp\');

async.parallel([
    bit         


        
相关标签:
3条回答
  • 2021-01-21 09:52

    (author of the npm module here)

    I solved it by adding my own counter at the end of the ms timestamp. It now supports up to 999 calls per ms because of this function. The first time it will generate something like 1409074885767000 and if you need a new nonce during the same ms it will then generate 1409074885767001, 1409074885767002, ...

    0 讨论(0)
  • 2021-01-21 10:03

    npm nonce module is now generate it correctly

    0 讨论(0)
  • 2021-01-21 10:10

    I had exactly the same problem, so I took askmike's code and modified it slightly.

    var nonce = new (function() {
    
        this.generate = function() {
    
            var now = Date.now();
    
            this.counter = (now === this.last? this.counter + 1 : 0);
            this.last    = now;
    
            // add padding to nonce
            var padding = 
                this.counter < 10 ? '000' : 
                    this.counter < 100 ? '00' :
                        this.counter < 1000 ?  '0' : '';
    
            return now+padding+this.counter;
        };
    })();
    

    use it like this

    nonce.generate();
    

    check out my jsfiddle with example

    0 讨论(0)
提交回复
热议问题