Search code examples
oauthsalesforce

How can i get a Refresh token using the Salesforce OAuth?


When i first authenticate with OAuth to Salesforce i dont get back a refresh token , i just get back an access token. So if the token needs to be refreshed by using the following:

var client = new RestClient("https://myOrg.salesforce.com/services/oauth2/token");
var request = new RestRequest("", Method.POST);
request.AddParameter("refresh_token", {currentRefreshToken}, ParameterType.GetOrPost);
request.AddParameter("grant_type", "refresh_token", ParameterType.GetOrPost);
request.AddParameter("client_id", {clientId}, ParameterType.GetOrPost);
request.AddParameter("client_secret", {clientsecret}, ParameterType.GetOrPost);
var response = client.Execute(request);

The currentRefreshToken is NULL because i didnt receive any refresh token on the initial authentication response. The initial call to authenticate is

    https://login.salesforce.com/services/oauth2/authorize? client_id={consumer_key}& 
    redirect_uri={callback_url}& response_type=code

and this doesnt give back a refreshtoken. How can i get an initial refresh token ?


Solution

  • The connected app has to allow issuing refresh tokens (check if "scopes" contains refresh and if there isn't something like "immediately expire refresh tokens" set).

    And then you need to ask for it, add optional scope=refresh_token parameter to your request.

    https://help.salesforce.com/s/articleView?id=sf.remoteaccess_oauth_user_agent_flow.htm&type=5