I am new Salesforce. I am trying to hit the below URL through the Rest client and I am passing the User name and Password as Basic Auth.


But Iam getting the response as below [ { "message": "Session expired or invalid", "errorCode": "INVALID_SESSION_ID" } ]

Not sure if Iam missing anything here.

Thanks in advance.

Regards, Srikanth.


First of all, you need to create a connected app in the instance in which you have the REST service created, and get the token by logging into Salesforce by hitting login URL (shown in the code below). You need to pass the parameters. Once you get the token, pass the token in the next request to salesforce and then it will return the appropriate result. Below is some basic code written in apex (another instance) to access the REST service written in some other instance of Salesforce.

public class AccountRestExampleConsumeFromSakshi{

    public String AccountRestExampleConsumeFromSakshi(){

    //hitting the login URL and get token
    String endpoint='https://login.salesforce.com/services/oauth2/token';

    //pass the parameters (Username, password, client id, client secret)    
    String username = 'username';
    String password = 'password#2';
    String ClientId= 'ClientIDfromConnecteApp';
    String ClientSecret = 'ClientSecretFromConnectedApp'; 

    Httprequest req = new HttpRequest();    

    req.setBody('grant_type=password' + 
          '&client_id=' + ClientId + 
          '&client_secret=' + ClientSecret + 
          '&username=' + username +
          '&password=' + password
    Http http = new Http();
    HttpResponse res;       
    String Access_Token;
    try {
        res = http.send(req);                
        JSONParser parser = JSON.createParser(res.getBody());
        while (parser.nextToken() != null){
            if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'access_token')) {
                    Access_Token = parser.getText();    
    catch(system.CalloutException e){            

    //we have got the token here.
    system.debug('access token'+Access_Token);

    Httprequest req1 = new HttpRequest();  
    req1.setHeader('Authorization','Bearer '+Access_Token);            
    Http http1 = new Http();
    HttpResponse res1 = http1.send(req1);                 
    return res1.getBody();
  • Thanks a lot for a quick response... So if I understood it correctly, for each rest call that we wish to make we need to create a new connected app ?? Also the URL that I mentioned earlier - github--staging.cs70.my.salesforce.com/services/apexrest/SBQQ/… points to a Managed package called Salesforce CPQ. So do i need to create the connected app even for managed package as well ?? Thanks in advance – Srikanth Dundigalla – Srikanth Dundigalla Jun 6 '17 at 10:19
  • no, only one connected app is enough, no need to create it again and again. Yes, you need to create connected app for managed package also, because connected app is for accessing a environment, doesn't matter how many services you have in that environment, and doesn't matter if it is in managed package or not. – Nitish Singhal Jun 6 '17 at 10:28
  • Sri, if it helped you then you can upvote and mark the answer. It will help others in future. – Nitish Singhal Jun 6 '17 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.