Coinbase API with javascript, unable to get correct authorisation

Viewed 86

I'm not that familiar with NodeJS which is why I've tried to adapt the code with jquery for the data to be integrated in my html. But the authorisation isn't working as i'm getting back 401. https://developers.coinbase.com/api/v2?shell#introduction

var apiKey = '****************';
var apiSecret = '********************************';

var timestamp;

$.ajax({
      url: "https://api.coinbase.com/v2/time",
      success: function(data){
        timestamp = data.data.epoch;
        return(timestamp);
      },
      error: function(err) {
      console.log(err.statusText)
      },
      dataType: 'json',
      async: false
});

var req = {
    method: 'GET',
    path: '/v2/accounts',
    body: ''
};


var message = timestamp + req.method + req.path + req.body;


var hash = CryptoJS.HmacSHA256("message", apiSecret);
hash = hash.toString(CryptoJS.enc.Hex)


$.ajax({
   url: "https://api.coinbase.com/v2/accounts",
   headers: {
       'CB-ACCESS-KEY': apiKey,
       'CB-ACCESS-SIGN': hash,
       'CB-ACCESS-TIMESTAMP': timestamp
   },
   success: function(data){
     console.log(data);
   },
   error: function(err) {
     console.log(err.statusText)
   },
   dataType: 'json',
});

I used CryptoJS to generate the hash (https://code.google.com/archive/p/crypto-js/). And as recommended by the cb docs I used their timestamp endpoint - Not sure if I am supposed to include the 'iso' or epoch' data? (I tried both)

Thank you for helping.

0 Answers
Related