Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

POST Request Basic Authorization – Not Working for Battle.net API in Node.js

I have formatted an axios post request as follows:

    var config = {
        method: 'post',
        url: 'https://us.battle.net/oauth/token',
        headers: { 
            'Authorization': 'Basic '+process.env.BATTLENET_CLIENT+':'+process.env.BATTLENET_SECRET, 
            ...data.getHeaders()
        },
        data : data
    };

This is getting rejected with a 401. However, when I generate the code snippet for this out of Postman, which functions, it is the same, except the Authorization is a seemingly random string that was generated:

var config = {
  method: 'post',
  url: 'https://us.battle.net/oauth/token',
  headers: { 
    'Authorization': 'Basic reallyRandomLongStringIsNotClientIDAndSecretKey=', 
    ...data.getHeaders()
  },
  data : data
};

Plugging this into my code made it work. I’m curious if there is something I’m missing when coding Basic Auth credentials, as it seems Postman has converted/encrypted it into something I can not figure out?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You just need to encode the string with username and password/secret to Base64 like this:

const encodedAuthorization = Buffer.from(`${process.env.BATTLENET_CLIENT}:${process.env.BATTLENET_SECRET}`).toString('base64')
 var config = {
        method: 'post',
        url: 'https://us.battle.net/oauth/token',
        headers: { 
            'Authorization': `Basic ${encodedAuthorization}`, 
            ...data.getHeaders()
        },
        data : data
    };
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading