+ 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 https://help.salesforce.com/apex/HTViewHelpDoc?id=remoteaccess_oauth_username_password_flow.htm&language=en

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="http://schemas.xmlsoap.org/soap/envelope/"><Header/><Body><login xmlns="urn:partner.soap.sforce.com"><username>' + userName+ '</username><password>' + pwd+ '</password></login></Body></Envelope>');
        Dom.XmlNode resultElmt = (new Http()).send(request).getBodyDocument().getRootElement()
          .getChildElement('Body', 'http://schemas.xmlsoap.org/soap/envelope/')
          .getChildElement('loginResponse', 'urn:partner.soap.sforce.com')
          .getChildElement('result', 'urn:partner.soap.sforce.com');

        final String SERVER_URL = resultElmt.getChildElement('serverUrl', 'urn:partner.soap.sforce.com') .getText().split('/services')[0];
        final String SESSION_ID = resultElmt.getChildElement('sessionId', 'urn:partner.soap.sforce.com') .getText();