You need to sign in to do that
Don't have an account?
David Massad
Named Credentials and Basic Authentication
I am having a problem with using named credentials for an HTTP callout. When I follow the example in the article titled, "Spring '15 - Simplify Authenticated Apex Callouts by Specifying Named Credentials as Endpoints" (https://help.salesforce.com/HTViewSolution?id=000206761&language=en_US" target="_blank), I get a 401 Unauthorized error from the server I'm trying to connect to.
I used Runscope to inspect the headers being sent to the server from Salesforce, and it seems that using named credentials is leaving off the "Basic" prefix from the Authorization header's value. For example, if your username is "abc" and your password is "123", using named credentials sends this in the header:
I used Runscope to inspect the headers being sent to the server from Salesforce, and it seems that using named credentials is leaving off the "Basic" prefix from the Authorization header's value. For example, if your username is "abc" and your password is "123", using named credentials sends this in the header:
Authorization: YWJjOjEyMw==Instead, it should be sending this for it to work:
Authorization: Basic YWJjOjEyMw==Is this a bug with Named Credentials?
Yes, I think it is a bug with Named Credentials in the article you shared.
The below code is working for me :
Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
Let me know if this any help for you ?
Cheers.
BD
public String endpoint_x = 'Callout:Availability****';
...
WebServiceCallout.invoke(
this,
request_x,
response_map_x,
new String[]{endpoint_x,);
response_x = response_map_x.get('response_x');
return response_x;
**************
// adding authorization
String authorizationHeader = 'BASIC ' +
EncodingUtil.base64Encode(Blob.valueOf(objAuthorization.username + ':' +
objAuthorization.password));