i am making a request to a endpoint in apex. i have encrypted data and key using crypto class, now i want to send the encrypted data and token id in the request header for authorization using GET method. the below code is not working and no data is getting pulled from the endpoint api.

(note: api using basic authorization is working fine.)

below is my code.

public static string encryptMessage(String message, String secretKey){
        Blob key = EncodingUtil.base64Decode(secretKey);
        Blob data = Blob.valueOf(message);
        Blob encrypted = Crypto.encryptWithManagedIV('AES128', key, data);
        return EncodingUtil.base64Encode(encrypted);

    String secretKey = 'xxxxxxxxxxxxxxxxxxxxxx==';
    String tokenId = 'xxxxxxxxxxxx';
    Datetime d = System.now();
    String payload = 'd='+d.format('yyyyMMddHHmmssSSSZ')+';c='+10+';t='+10;
    String message = tokenId + ':' + encryptMessage(payload, secretKey);

    HttpRequest req = new HttpRequest();
    req.setEndpoint('my endpoint url');
    req.setHeader('Accept', 'application/xml;charset=UTF-8'); 
    req.setHeader('Authorization', 'OAuth' +     EncodingUtil.base64Encode(blob.valueOf( message)));

Http http = new Http();
HTTPResponse res = http.send(req);
string response = res.getBody();
  • What does the response look like? responseCode, header and body – human Nov 4 '16 at 9:48

If you are following Oauth then in Oauth 2 we do something like this.

string authorizationHeader = 'Bearer '+EncodingUtil.base64Encode(blob.valueOf( message));

So you might need to change your code.

  • i tired, its not working – Ritz Arlekar Nov 4 '16 at 6:37
  • @RitzArlekar are you getting any error? if yes then post that. Also which authentication protocol you are using Oauth 2 or Basic? – Tushar Sharma Nov 4 '16 at 6:39
  • i am unaware of the authentication protocol to be used for 'AES128' encryption. that is what i want to know. but certainly not using Basic. – Ritz Arlekar Nov 4 '16 at 8:41
  • @RitzArlekar you can confirm that from the documentation of site i which you are making request. SF support oauth2.0 – Tushar Sharma Nov 4 '16 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.