javascript - Using $http.get to retrieve from API(zendesk) not working -


i'm trying make api call zendesk's api , keep getting 401 auth code though same thing works when curl in terminal. how can make work in angular?

function dataservice($http) {     var service = {         getmacros: getmacros     };      return service;     /////////////////////      function getmacros() {          var client = {            username: window.btoa('myemail'),            token: window.btoa('/token:mytoken'),            remoteuri: 'https://mycompany.zendesk.com/api/v2/macros.json'         };          console.log('loading...');         return $http({                method: 'get',                url: client.remoteuri,                headers: {                    'authorization': client.username + client.token               }             })                             .then(getmacroscomplete)             .catch(function (message) {                 exception.catcher('failed getmacros')(message);             });          function getmacroscomplete(response) {             console.log('done');             var data = response.data;             return data;         };     }; 

the code above returns 401 while works:

curl myemail/token:mytoken https://mycompany.zendesk.com/api/v2/macros.json  

seems work well. obvious.

the thing need set authorization headers properly, should base64 encoded , have state "basic" authentication (this main part people miss).

so should work:

function dataservice($http) {      var service = {          getmacros: getmacros      };        return service;      /////////////////////        function getmacros() {            var client = {             username: 'myemail',             token: 'mytoken',             remoteuri: 'https://mycompany.zendesk.com/api/v2/macros.json'          };            console.log('loading...');          return $http({                 method: 'get',                 url: client.remoteuri,                 headers: {                     'authorization': 'basic ' + window.btoa(client.username + '/token:' + client.token)                }              })                              .then(getmacroscomplete)              .catch(function (message) {                  exception.catcher('failed getmacros')(message);              });            function getmacroscomplete(response) {              console.log('done');              var data = response.data;              return data;          };      };

of course have have token authentication enabled in zendesk account, otherwise can authenticate via user + password setting password instead , doing:

'authorization': 'basic ' + window.btoa(client.username + ':' + client.password)


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -