function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
George AdamsGeorge Adams 

Help with Basic HTTP Authentication (pretty simple question)

Hello,

I can't get this code to work. I keep getting the response, "{"Code":50,"Message":"Must supply a valid HTTP Basic Authorization header"}".

I'm trying to get data from Campaign Monitor, as shown here.

Here is the code I'm using:
Http httpProtocol = new Http();
HttpRequest request = new HttpRequest();
request.setEndPoint('https://api.createsend.com/api/v3.1/clients.json');


Blob headerValue = Blob.valueOf('u:' + '34534j5hk34j5hk34j5hk3j45hk3j'); //not my actual API key :)
String authorizationHeader = EncodingUtil.base64Encode(headerValue);
request.setHeader('Authorization', 'Basic ' + authorizationHeader);

//I've also tried it like this:
request.setHeader('Authorization', authorizationHeader);

request.setMethod('GET');
HttpResponse response = httpProtocol.send(request);
System.debug(response.getBody());

Are my headers missing info? I can't firgure out what it's looking for.

Thanks!
Best Answer chosen by George Adams
George AdamsGeorge Adams
For anyone seeing this in the future, here's the working authentication code:
Http httpProtocol = new Http();
HttpRequest request = new HttpRequest();
request.setEndPoint('https://api.createsend.com/api/v3.1/clients.json');


Blob headerValue = Blob.valueOf('ssdf8s7d6f8s7d6f8s7d6f87s6df87sfd:x');
String authorizationHeader = EncodingUtil.base64Encode(headerValue);
request.setHeader('Authorization', 'Basic ' + authorizationHeader);

request.setMethod('GET');
HttpResponse response = httpProtocol.send(request);
System.debug(response.getBody());

 

All Answers

George AdamsGeorge Adams
For anyone seeing this in the future, here's the working authentication code:
Http httpProtocol = new Http();
HttpRequest request = new HttpRequest();
request.setEndPoint('https://api.createsend.com/api/v3.1/clients.json');


Blob headerValue = Blob.valueOf('ssdf8s7d6f8s7d6f8s7d6f87s6df87sfd:x');
String authorizationHeader = EncodingUtil.base64Encode(headerValue);
request.setHeader('Authorization', 'Basic ' + authorizationHeader);

request.setMethod('GET');
HttpResponse response = httpProtocol.send(request);
System.debug(response.getBody());

 
This was selected as the best answer
JeffreyStevensJeffreyStevens
so - how did you figure it out?  Documentation on the Campaign Monitor?
George AdamsGeorge Adams
I found this post (https://www.campaignmonitor.com/forums/topic/5711/must-supply-a-valid-http-basic-authorization-header-classic-asp/) on the Campaign Monitor forums, and looked at the post by user jamesd.

That basically set me on the right track of exactly what to convert to base64.