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
ElmarElmar 

Basic http authentication for Salesforce?

Hi,

 

is there a way to authenticate an application for using the salesforce rest api with just the normal username / password credentials from

the basic http authentication?

I know thats not safe but its just for testing the functionality of my application.

 

best regards

Elmar

Navatar_DbSupNavatar_DbSup

Hi,

Try the below code snippet as reference:

HTTP h = new HTTP();

HTTPRequest r = new HTTPRequest();

r.setEndpoint('https://my.domain.com');

Blob headerValue = Blob.valueOf(username + ':' + password);

String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);

r.setHeader('Authorization', authorizationHeader);

r.setMethod('GET');

HTTPResponse resp = h.send(r);

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

ElmarElmar

hi,

 

what is the salesforce endpoint for the basic http authentication?

for the oauth2 it is https://na1.salesforce.com/services/oauth2/token, right?

and what about the credentials, do i have to use the username password from my developer account?

thx for your help!

 

best regards

Elmar

SuperfellSuperfell

You cannot use basic auth to make requests to the salesforce.com REST API.

hemantgarghemantgarg

Hi,

 

I am surprised to see that it does not work sometimes, I got into serious issue with the word "BASIC " , I changed it to "Basic " and it worked for me.

 

Chetu PartnerChetu Partner
Thanks, this code worked for me.

Hi,
Try the below code snippet as reference:
HTTP h = new HTTP();
HTTPRequest r = new HTTPRequest();
r.setEndpoint('https://my.domain.com');
Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
r.setHeader('Authorization', authorizationHeader);
r.setMethod('GET');
HTTPResponse resp = h.send(r);
Sankalita Chatterjee 19Sankalita Chatterjee 19
A BIG thanks to hemantgarg who just saved my day. I indeed couldn#t make it work with BASIC, but just changed it to Basic and everything worked! It is weird and very confusing as Salesforce docs say "BASIC": https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_httprequest.htm
Ken Koellner 1Ken Koellner 1
Don't the code examples above demonstrate calling out from Apex using HTTP Basic auth?  Was the original requestion about calling INFO salesforce?  I have a use case where I'd like to make calls into Rest service implemented in Apex using Basic Auth.  My guess is there's no way to do it but I thought I'd ask on this thread. 
sfdcFanBoysfdcFanBoy
You need to use Named Credential for that.  Here's the code and how to set up.  This is very easy.

Named Credentials - Setup (https://sfdcfanboy.com/2018/01/15/named-credentials-setup/)

Named Credentials - Code (https://sfdcfanboy.com/2018/01/15/named-credentials-code/)
 
HTTPRequest feedRequest = new HTTP Request();
feedRequest.setEndpoint('callout:SalesforceNC/services/data/v32.0');
feedRequest.setMethod('GET');

 
RaNa_SFRaNa_SF
Hi Everyone on the thread. I'm new to the Salesforce Integration. I have a similar request where we have to make a REST api call to external system using Basic Auth as the external application does not support OAuth.As suggested above. I have created the Named Credentials and the Http callout. What are the next steps or the information to be sent out to the external system to connect to Salesforce?

Thanks in advance.
Sachin Khanna 12Sachin Khanna 12
I use the xmlHttpRequest and it works for me !!!.   
let userName = document.getElementById('username').value;
    let password = document.getElementById('password').value;
    console.log(encodeURIComponent(password));
    var url = 'https://ap24.salesforce.com/index.jsp?un='+encodeURIComponent(userName)+'&pw='+encodeURIComponent(password);
    var xhr = new XMLHttpRequest();
    xhr.open("POST",url);
    xhr.setRequestHeader('Accept', 'application/json');
    xhr.send();
    console.log(xhr.responseText);
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            console.log(xhr.response);
            if(xhr.responseURL == url){
                console.log("Error");
            }else{
                console.log("Successfully Login");
                window.open(xhr.responseURL);
            }
        }
    };
Admin Admin 750Admin Admin 750
Shoutout to say thanks to hemantgarg, changing BASIC to Basic did the trick