Activity Forums Salesforce® Discussions How to get the access token or session id for integration by using standard API in Salesforce?

  • Piyush

    Member
    January 11, 2018 at 8:23 am

    Hi Abhishek,

    You can get the session id using this:-

    String sessionID = UserInfo.getSessionId();

    You can get the accessToken using this:-

    Create connected app then fill all the details then call this https method it will  provide you access token

    public final String clientId ='';
    public final string clientSecret='';
    public final String userName = '';
    public final String password = '';
    HttpRequest req = new HttpRequest();
    req.setMethod('POST');
    req.setEndpoint(endPointUrl);
    req.setBody('grant_type=password' +
    '&client_id='+ clientId +
    '&client_secret='+ clientSecret +
    '&username=' + EncodingUtil.urlEncode(userName, 'UTF-8') +
    '&password=' + EncodingUtil.urlEncode(password, 'UTF-8'));
    Http h = new Http();
    Httpresponse res = h.send(req);
    string body = res.getBody();

    system.debug('***** Body -- '+ body);

    wrapperForHttp wObj = (wrapperForHttp)Json.deserialize(body,wrapperForHttp.class);
    string accessToken = wObj.access_token;
    system.debug('***** accessToken -- '+ accessToken);

     

    Hope this help you..

    • This reply was modified 6 years, 11 months ago by  Piyush.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos