salesforce rest api INVALID_SESSION_ID error

49,492

Solution 1

How are you passing the sessionId to the /services/data/v20.0 request?, if your access_token is abc123 then you need a http header of Authorization: OAuth abc123 in the request.

Solution 2

API session in salesforce expires regardless if there are activities or not. to set the duration of each session go to Setup > Administration Setup > Security Controls > Session Settings>

the max is 8hours.

cheers!

Share:
49,492
Saums
Author by

Saums

Updated on February 03, 2022

Comments

  • Saums
    Saums about 2 years

    I am using salesforce rest api to access the salesforce account from my rails app.I created a remote access app and got the key N the id. I was able to authenticate the user and get the auth_token, instance url and all that. But, when I send request at "instance_url/services/data/v20.0" along with the access token , I get this error:

    [{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]
    

    I have a developer salesforce account and have API enabled true for every profile, except for the "Authenticated website" profile(which is not accessible).

    Please, can anybody help me with this?

    I'm authenticating the user with following request

    HTTParty.post "login.salesforce.com/services/oauth2/token";, :body=>{"grant_type"=>"authorization_code","code"=>"abc}","client_secret"=>"abc"‌​, "client_id"=>"abc","format"=>"json","redirect_uri"=>"localhost:3000/salesforce/callback";}
    

    which is returning signature, id, instance_url, issued_at, access_token and refresh_token

    HTTParty.get "ap1.salesforce.com/services/data/v20.0";, :headers=>{"Authentication"=>"OAuth access_token", "Content-Type"=>"application/json"}
    

    which responds with

    [{"errorCode"=>"INVALID_SESSION_ID", "message"=>"Session expired or invalid"}]
    
    • lnediger
      lnediger almost 13 years
      Do you have remote access properly configured in your Salesforce org? It might also help if you posted your login/query code.
  • Saums
    Saums almost 13 years
    I don't think thats the problem bcoz the session time is set to 2hrs and i am sending the request immediately after getting the access_token...............Also I m gettin successful response if i send request for the api versions............."instance_url/services/data/"(with access_token wrapped up in the header) returns [{"label"=>"Winter '11", "url"=>"/services/data/v20.0", "version"=>"20.0"}, {"label"=>"Spring '11", "url"=>"/services/data/v21.0", "version"=>"21.0"}].............Anything else i shud look for???
  • Saums
    Saums almost 13 years
    I m gettin successful response if i send request for the api versions............."instance_url/services/data/"(with access_token wrapped up in the header) returns [{"label"=>"Winter '11", "url"=>"/services/data/v20.0", "version"=>"20.0"}, {"label"=>"Spring '11", "url"=>"/services/data/v21.0", "version"=>"21.0"}].............Anything else i shud look for???
  • superfell
    superfell almost 13 years
    the versions resource doesn't require authentication, so that proves nothing. Why don't you post some code and/or your actual request.
  • Saums
    Saums almost 13 years
    i'm authenticating the user with following request.......HTTParty.post "login.salesforce.com/services/oauth2/token", :body=>{"grant_type"=>"authorization_code","code"=>"abc}","c‌​lient_secret"=>"abc"‌​, "client_id"=>"abc","format"=>"json","redirect_uri"=>"localho‌​st:3000/salesforce/c‌​allback"}...........‌​which is returning me signature,id,instance_url,issued_at,access_token and refresh_token
  • Saums
    Saums almost 13 years
    Then i'm sending request for available resources .........HTTParty.get "ap1.salesforce.com/services/data/v20.0", :headers=>{"Authentication"=>"OAuth access_token", "Content-Type"=>"application/json"}.......which responds with [{"errorCode"=>"INVALID_SESSION_ID", "message"=>"Session expired or invalid"}]..............anything wrong there???
  • superfell
    superfell almost 13 years
    as per my answer above, the header is Authorization, not Authentication.
  • Bhushan Lodha
    Bhushan Lodha over 11 years
    @Saums: could you tell exactly what was the solution? I am getting same error nd I am using databasedotcom gem