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
nagnknagnk 

Using RESt,I have external app user id password and security key and app code and service URL cloud please tell me how to get token please post code

Alexander TsitsuraAlexander Tsitsura
Hi,

Please see example below
String clientId = '*************************************************************************************************;
String clientSecret = '*********************';
String username = '****@*****.*****';
String password = '**************';
String token = '**************************************';

HttpRequest req = new HttpRequest();
req.setMethod('POST');
req.setEndpoint('https://login.salesforce.com/services/oauth2/token');
req.setBody('grant_type=password' +
    '&client_id=' + clientId +
    '&client_secret=' + clientSecret +
    '&username=' + EncodingUtil.urlEncode(username, 'UTF-8') +
    '&password=' + EncodingUtil.urlEncode(password, 'UTF-8'));

Http http = new Http();
HTTPResponse res = http.send(req);
System.debug('BODY: '+res.getBody());
System.debug('STATUS: '+res.getStatus());
System.debug('STATUS_CODE: '+res.getStatusCode());
System.debug('CONTENT_TYPE: '+res.getHeader('Content-Type'));

As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you.

Thanks,
Alex