Created
May 23, 2016 08:57
-
-
Save RatanPaul/255dd68cc8b2164bb5e6acd216e82cbc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String username=''; | |
String pwd=''; | |
HttpRequest request = new HttpRequest(); | |
request.setEndpoint('https://www.salesforce.com/services/Soap/u/32.0'); | |
request.setMethod('POST'); | |
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(); | |
final PageReference theUrl = new PageReference(SERVER_URL + '/services/data/v36.0/sobjects/Account/'); | |
request = new HttpRequest(); | |
request.setEndpoint(theUrl.getUrl()); | |
request.setHeader('Content-Type', 'application/json'); | |
request.setMethod('GET'); | |
request.setHeader('Authorization', 'Bearer '+SESSION_ID); | |
String body = (new Http()).send(request).getBody(); | |
system.debug('=body ======='+body ); | |
JSONParser parser = JSON.createParser(body); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can we do this without using username password? if SSO is set up?