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
Sanchivan SivadasanSanchivan Sivadasan 

Calling Apex Rest API without oAuth

Hi there,

I have an Apex Rest Webservice that I need to call but without oAuth. I want to use my username and password to authenticate. Is this possible? If yes, how can I do this? Assume that I will be calling this from either Java or C#. Thanks.


Sanchivan SivadasanSanchivan Sivadasan
Hi there,

I found this link

What is the client_id and client_secret that needed to be passed? 
Hi if you want you use your username and password to call Apex Rest API then you will have to use Username-Password Flow. 
First of all you will have to create a connected app to get client_id and client_secret then you can given steps in the given link.
You can also directly make a http login call to salesforce by passing the required details like below

    String pwd='';
    String userName='';

HttpRequest request= new HttpRequest();
        request.setHeader('Content-Type', 'text/xml;charset=UTF-8');
        request.setHeader('SOAPAction', '""');
        request.setBody('<Envelope xmlns=""><Header/><Body><login xmlns=""><username>' + userName+ '</username><password>' + pwd+ '</password></login></Body></Envelope>');
        Dom.XmlNode resultElmt = (new Http()).send(request).getBodyDocument().getRootElement()
          .getChildElement('Body', '')
          .getChildElement('loginResponse', '')
          .getChildElement('result', '');

        final String SERVER_URL = resultElmt.getChildElement('serverUrl', '') .getText().split('/services')[0];
        final String SESSION_ID = resultElmt.getChildElement('sessionId', '') .getText();