0

I need help converting this piece of python to javascript.

auth = "Basic " + base64.b64encode(bytes(IdentityClientId + ":" + IdentityClientSecret, "utf-8")).decode('ascii')

What I've tried:

var auth = "Basic " + unescape(btoa(IdentityClientId + ":" + IdentityClientSecret));

No luck so far.

jinx
  • 87
  • 1
  • 7
  • 1
    Could you explain a little bit more what's your problem? What does not work here? – sjahan Jun 14 '18 at 09:21
  • Basically just to convert the python to Javascript, the Javascript portion does not parse the same as the python one. – jinx Jun 14 '18 at 10:25
  • I'm not a python expert, but from what i tried, i get the same result for both version. I suspect the problem comes from encoding: in your login and password, are there any special chars that could mess it up? – sjahan Jun 14 '18 at 11:31

1 Answers1

1

As suspected in the comment, you have some problem with Unicode characters.

You can use this to encode properly the characters:

function b64EncodeUnicode(str) {
    return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
        return String.fromCharCode(parseInt(p1, 16));
    }));
}

console.log(b64EncodeUnicode('lögin:password'));

Check this post for more details about this!

sjahan
  • 5,720
  • 3
  • 19
  • 42