I'm struggling with a requirement. I have to generate an access_token with "header" and "payload" information encoded inside the value of the token, all of this using salesforce. I understand this is related to a JWT token.

I have already generate a OAuth 2.0 JWT Bearer Flow to create the JWT and to get then an access_token but the problem is that this acces_token doesn't have the JWT format so doesn't have the required "header" and "payload" information encoded inside as the requirement asqued for.

I know that this can be achive but and don't know if it is posible to achive expecificaly in salesforce.

If anyone have some information or know how to achive that on salesforce I appreciate the help.


1 Answer 1


I have some code below that I use to create a JWT for an external service - you might be able to use this:

public static string getJWTStringLive() {
    String secret = 'secret goes here';

    String header = '{"alg":"HS256","typ":"JWT"}';
    String header_encoded = EncodingUtil.base64Encode(blob.valueof(header));
    header_encoded = header_encoded.replaceAll('=', '');
    header_encoded = header_encoded.replaceAll('-', '');
    header_encoded = header_encoded.replaceAll('/', '');

    String claim_set =
      '{"iss":"issgoeshere","iat":"' +
      datetime.now().getTime() / 1000 +
    String claim_set_encoded = EncodingUtil.base64Encode(
    claim_set_encoded = claim_set_encoded.replaceAll('=', '');
    claim_set_encoded = claim_set_encoded.replaceAll('-', '');
    claim_set_encoded = claim_set_encoded.replaceAll('/', '');

    String jwtRequest = header_encoded + '.' + claim_set_encoded;

    Blob signature_blob = Crypto.generateMac(
    string signature_string = EncodingUtil.base64Encode(signature_blob);
    signature_string = signature_string.replace('/', '_');
    signature_string = signature_string.replace('+', '-');
    signature_string = signature_string.replace('=', '');
    String finalJWT = jwtRequest + '.' + signature_string;

    return finalJWT;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .