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
Veera GoriparthiVeera Goriparthi 

Session expired or invalid

Hi,
I am using REST API call to get SF account details. I am able to get OAuth token, but when I use this token to get Account details it gives below rror.

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



Here  is the flow

Request:https://ap1.salesforce.com/services/oauth2/token?grant_type=authorization_code&code=aPrxqJ8A8kLOza8Stjpv6sovqPjwgt9wJxzcIaPRsCnGPyxvpLJSOUhmEHkQ7qCNO84np7gXZQ%3D%3D&client_id=3MVG9ZL0ppGP5UrBx8LU.vAPd80ozIo4JMB66N3r1IPzsiMMPzIYm3pklapNDhi24wcd3Ik8JtlQZqr_opatH&client_secret=5390506801126798324&redirect_uri=https://local/user/connector/generic/redirectoauth


Response:
{"id":"https://login.salesforce.com/id/00D28000000KMl1EAG/00528000000MwpQAAS","issued_at":"1428631322872","scope":"id full custom_permissions api web openid visualforce refresh_token chatter_api","instance_url":"https://ap2.salesforce.com","token_type":"Bearer","refresh_token":"5Aep861TSESvWeug_wBae_.NiEYtLVbgA9FW1UDMAq7zqGYpEeyCgRS1jLbqFuSuC2vJd5RyDOmpU.pSPn.Q1zV","id_token":"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjE5NCJ9.eyJleHAiOjE0Mjg2MzE0NDIsInN1YiI6Imh0dHBzOi8vbG9naW4uc2FsZXNmb3JjZS5jb20vaWQvMDBEMjgwMDAwMDBLTWwxRUFHLzAwNTI4MDAwMDAwTXdwUUFBUyIsImF0X2hhc2giOiJlbF9iOXBzSlJCMVFOMGI5cXJib0V3IiwiYXVkIjoiM01WRzlaTDBwcEdQNVVyQng4TFUudkFQZDgwb3pJbzRKTUI2Nk4zcjFJUHpzaU1NUHpJWW0zcHRsYXBORGhpMjR3Y2QzSWs4SnRsUVpxcl9vcGF0SCIsImlzcyI6Imh0dHBzOi8vbG9naW4uc2FsZXNmb3JjZS5jb20iLCJpYXQiOjE0Mjg2MzEzMjJ9.XXfxxvNwQgWrSP-eFoUhFbzDfqxsASntM1slTjhQXN_vgHAp-Wv0rJYBWg6QXcIWwQEsvfCAvGi9JkbRIZ5UYqLPSrnhxsje4StPfvwwHMBMarimqeKKWt24Xb6Hri0DMdp6FjN9y9RneJpVcZeLHbkFnOIUf11cgMk1d3kI9KrgmkoDn8TI1yfPU0NLbJtafnYw2S9cdHrcju31i-9eLjy-yeMrJtinS5TqUdWcalEL1uqZq_KGEnsWkmN8sjEaSgtCio33ZZwDL6IKTgdHeDZedEZfRqhUoyBni14qYdpRX9ANSd1B61HtCVLiAeetc156KB4KYr-oR0Byg7oqAswhU41OQ9RmNp5VDQVVKspgwImauJzhpLs59W64SH5dP
h4xn5SF7fCGK534idYP_HQOey2KxBNrS5ATOATbNHsDt0maztiPK0K8rNjjnARya8QCqPJoumeJddchsmYhMIWPrxtwzNdpPXMuSz3hY3xIALFf0cDW_OwBPWT4P3_KZqfE7w0lPihzwhTwHy4tSu_kq4t4ikMu-xc6TNkZoy4HJP_qNZ7f7CCENHvUHjF_vgjSqNcPVJnpp-g3ImN6eBbstsndoktkdxpSC8N34WyWwKxiC5S5NVtgJBbLI2J9Q8xdBvcArvvoANt6_nhjsDf6417kRstohZio1aOoSW8","signature":"3yrSoEaIoCEtGXZpMHaLnV8B6z/njamvHSPTOHUB9ME=","access_token":"00D28000000KMl1!ARAAQPLIuaRCKyXQPB1LDjuiF4Bg1QAVfzXNFpq7C4zYRVurh3JxqYpcu5IIf98P1XOB7oEwLkbSJwdq4Mc0pxkmaQCbjbgd"}

token 00D28000000KMl1!ARAAQPLIuaRCKyXQPB1LDjuiF4Bg1QAVfzXNFpq7C4zYRVurh3JxqYpcu5IIf98P1XOB7oEwLkbSJwdq4Mc0pxkmaQCbjbgd
Request:https://ap1.salesforce.com/services/data/v31.0/query?q=SELECT+Name+From+User+WHERE+Id='00528000000MwpQAAS'
Authorization:Bearer 00D28000000KMl1!ARAAQPLIuaRCKyXQPB1LDjuiF4Bg1QAVfzXNFpq7C4zYRVurh3JxqYpcu5IIf98P1XOB7oEwLkbSJwdq4Mc0pxkmaQCbjbgd

Error while sending Salesforce request {}HTTP_401 : [{"message":"Session expired
 or invalid","errorCode":"INVALID_SESSION_ID"}]


Thanks,
Veera.
NagaNaga (Salesforce Developers) 
Hi Veera,


You can authenticate using the session ID as well for the REST API. However, you need to add a word "Bearer " infront of the session Id.

e.g.: req.setHeader('Authorization',  'Bearer sessionid_here');

This page has some more details: http://www.salesforce.com/us/developer/docs/api_rest/index_Left.htm#CSHID=quickstart_oauth.htm|StartTopic=Content%2Fquickstart_oauth.htm|SkinName=webhelp

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.

Best Regards
Naga Kiran
Meenakshi PMeenakshi P
Can you please tell me what is the instance url we need to give during authorization?
Is this correct url here in below request? I also get same invalid session_id error.
curl -k -v https://na24.salesforce.com/services/data/v20.0/ -H 'Authorization: Bearer 00D1a000000Jb4Z!ARkAQChPkjf9B0psZYY7tQIJRl3SgrIyher9mDTJ3PnjI6fU_KN8pMbb
CKtYPSaZUwll.9St7WFjWqJtY0J5ld.3XTrBxp_x'