I solved it! I did not understand that the signature itself should be encoded in base64. In addition, I needed to set the last optional parameter of the hash_hmac function to $raw_output=true (see the documents . In short, I needed to change my code from the original:
//build the signature $key = 'secret'; $signature = hash_hmac('SHA256',"$headers_encoded.$payload_encoded",$key); //build and return the token $token = "$headers_encoded.$payload_encoded.$signature";
To the corrected:
//build the signature $key = 'secret'; $signature = hash_hmac('SHA256',"$headers_encoded.$payload_encoded",$key,true); $signature_encoded = base64url_encode($signature); //build and return the token $token = "$headers_encoded.$payload_encoded.$signature_encoded"; echo $token;
source share