+ Start a Discussion

Get refresh token automatically

I have been googling a lot about the fact how to get a refresh token. I have rest integration between salesforce to salesforce and it is working fine. After few hours not using the service I get this message in my debug log :
message: System.HttpResponse[Status=Unauthorized, StatusCode=401]
Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]

I totally understand why it is happening ( cause every access has some time limit ). Now i want to request for the refresh token ( I understood by reading multiple articles. sample). In order to request for a refresh token, we should pass some parameters as mentioned below.
POST /services/oauth2/token HTTP/1.1
Host: https://login.salesforce.com/ 
&refresh_token=***your token here***
But just wanted to know that from where I get &refresh_token=your token here mentioned in the above sample code.
The rest integration is setup using Named credentials and Auth providers.

As a work around now, when the session is expired every time, I go to connected app and re-authenticate and my code works just fine.

My goal here is, when i face this Session Expired message, i should be able refresh the token withing the same class where i am invoking the rest service so that other piece of logic works fine.

My connected app looks like this and i am doubting that i should give scope as full +refresh token. Am i correct? please let me know if anyone has suggestions.

User-added image
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Chandrakanth,

May I suggest you please refer the below link for reference. Let us know if it helps.

Please mark it as best answer if the information is informative.so that question is removed from an unanswered question and appear as a proper solution.

Rahul Kumar