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
Storm17Storm17 

Refresh token always returns "expired access/refresh token"

Hello

 

I'm working on an app in java (Android) and I'm trying to use a refresh token to obtain a new OAuth access token.  I got the access token through the SalesForce Android toolkit, and this logs me in fine for the session, but when I use the refresh token to try to get a new access token, I get the following error:

 

{"error":"invalid_grant","error_description":"expired access/refresh token"}

 

My consumer key and secret both seem to be fine, because I get the appropriate errors when I alter them.  Has anyone else been getting this error?  Is it something to do with the URL encoding?

 

Here is the code I'm using:

 

      HttpPost httppost = new HttpPost(
          "https://login.salesforce.com/services/oauth2/token");

      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
      nameValuePairs.add(new BasicNameValuePair("grant_type", "refresh_token"));
      nameValuePairs
          .add(new BasicNameValuePair("client_id", OAUTH_CONSUMER_KEY));
      nameValuePairs.add(new BasicNameValuePair("client_secret",
          OAUTH_CONSUMER_SECRET));
      nameValuePairs
          .add(new BasicNameValuePair("refresh_token", REFRESH_TOKEN));
      nameValuePairs.add(new BasicNameValuePair("format", "json"));

      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

      HttpClient client = new DefaultHttpClient();
      HttpResponse response = client.execute(httppost);
      HttpEntity entity = response.getEntity();

      String responseText = EntityUtils.toString(entity);

Best Answer chosen by Admin (Salesforce Developers) 
Storm17Storm17

I figured it out.  The refresh token was provided to me URL encoded, so the last two characters, ==, were shown as %3D%3D.  I'll have to decode the token before storing it.

All Answers

Storm17Storm17

I figured it out.  The refresh token was provided to me URL encoded, so the last two characters, ==, were shown as %3D%3D.  I'll have to decode the token before storing it.

This was selected as the best answer
samirvbayanisamirvbayani

Can you elaborate what you changed to get this working? I am facing the same error in my case.

Alvin Dutt 6Alvin Dutt 6
Hi please can you elaborate on your answer about refresh token