How to Use Base 64 Encoding
The auth code and client credentials grants require the auth code to be passed in the Authorization header using base 64 encoding. Many HTTP/REST libraries will handle the formatting and encoding for basic authentication requests, though not all do. This page serves to provide an explanation of the encoding process.
The Authorization Header
The Authorization header is the format Authorization: Basic encodedString
, where encodedString is the result of base 64 encoding the OAuth client's values as clientId:clientSecret
.
Examples
The following code examples demonstrate how to build the Authorization header in the case that a HTTP library does not perform this function.
Java
The javax.xml.bind.DatatypeConverter class can be used to convert byte data into a base 64 encoded string.
String clientId = "a0bda580-cb41-4ff6-8f06-28ffb4227594";
String clientSecret = "e4meQ53cXGq53j6uffdULVjRl8It8M3FVsupKei0nSg";
String encodedData = DatatypeConverter.printBase64Binary((clientId + ":" + clientSecret).getBytes("UTF-8"));
String authorizationHeaderString = "Authorization: Basic " + encodedData;
C\
C# handles base 64 encoding by getting a byte array from the string and converting the byte array to a base 64 encoded string.
var clientId = "a0bda580-cb41-4ff6-8f06-28ffb4227594";
var clientSecret = "e4meQ53cXGq53j6uffdULVjRl8It8M3FVsupKei0nSg";
var encodedData = System.Convert.ToBase64String(
System.Text.Encoding.GetEncoding("ISO-8859-1")
.GetBytes(clientId + ":" + clientSecret)
);
var authorizationHeaderString = "Authorization: Basic " + encodedData;
JavaScript (web)
In web applications, the window
variable contains a function btoa(stringToEncode) to base 64 encode a string. Try it live at jsfiddle!
var clientId = 'a0bda580-cb41-4ff6-8f06-28ffb4227594';
var clientSecret = 'e4meQ53cXGq53j6uffdULVjRl8It8M3FVsupKei0nSg';
var encodedData = window.btoa(clientId + ':' + clientSecret);
var authorizationHeaderString = 'Authorization: Basic ' + encodedData;
JavaScript (node.js)
In node.js, there is no window
variable, so the btoa function is not present. Encoding can be accomplished manually, however, by using the Buffer class and converting the buffer to a base 64 string.
var clientId = 'a0bda580-cb41-4ff6-8f06-28ffb4227594';
var clientSecret = 'e4meQ53cXGq53j6uffdULVjRl8It8M3FVsupKei0nSg';
var encodedData = Buffer.from(clientId + ':' + clientSecret).toString('base64');
var authorizationHeaderString = 'Authorization: Basic ' + encodedData;
Terminal
In OSX/Linux, the terminal can be used to encode a string. This is useful for development purposes, but is not recommended for production as this is not a secure method. In some use cases, a newline character may be appended to the resulting string. This character is not part of the encoded value and should be removed. In some terminal environments, the base64
command can be used with the -w 0
option to remove this additional whitespace.
echo -n a0bda580-cb41-4ff6-8f06-28ffb4227594:e4meQ53cXGq53j6uffdULVjRl8It8M3FVsupKei0nSg | base64
Python
The base64
package can be used to encode the client ID and secret.
import base64
client_id = "a0bda580-cb41-4ff6-8f06-28ffb4227594"
client_secret = "e4meQ53cXGq53j6uffdULVjRl8It8M3FVsupKei0nSg"
encodedData = base64.b64encode(bytes(f"{client_id}:{client_secret}", "ISO-8859-1")).decode("ascii")
authorization_header_string = f"Authorization: Basic {encodedData}"