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
DharmendraDharmendra 

Sometimes Refresh token expired while retrieving new access token.

Hi,

I'm receiving refresh token expired/invalid error while retrieving the new access token. This issue is occurring sometimes (as per what I've noticed it is occurring when no action is taken for last 1 day or more.) otherwise it works.
Please suggest me to solve this issue.

Below is code sample for retrieving the new access token using existing refresh token.
string grant_type = "refresh_token";
string data = "client_id={0}&refresh_token={1}&grant_type={2}&granularity={3}";
HttpWebRequest request = HttpWebRequest.Create(_loginUrl) as HttpWebRequest;
string result = string.Empty;
string param = string.Format(data, _clientId, refreshToken, grant_type, "best");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ProtocolVersion = HttpVersion.Version11;
var bs = System.Text.Encoding.UTF8.GetBytes(param);
using (Stream reqStream = request.GetRequestStream())
{
	reqStream.Write(bs, 0, bs.Length);
}
using (WebResponse response = request.GetResponse())
{
	using (var sr = new StreamReader(response.GetResponseStream()))
	{
		result = sr.ReadToEnd();
		sr.Close();
	}
}
if (!string.IsNullOrEmpty(result))
{
	var jsonSerializer = new JavaScriptSerializer();
	var tokenData = jsonSerializer.Deserialize<SalesForceAccessModel>(result);
	return tokenData.Access_Token;
}

 
Best Answer chosen by Dharmendra
DharmendraDharmendra
I was using 2 systems and one is making calls with wrong client Id and screte keys. Its fixed by using correct credentials.