Reply
Visitor
PT Developer
Posts: 1
0

"expired access/refresh token" while trying to authentificate

We try to download a file provided by salesforce with simple java client. For that request authentication is required. But the URL call always returns "expired access/refresh token".

 

The source code:

 

    public static void connect()
    {
        HttpClient httpclient = new DefaultHttpClient();
        HttpParams httpParams = httpclient.getParams();
        HttpClientParams.setCookiePolicy(httpParams, CookiePolicy.RFC_2109);
        httpParams.setParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 30000);

        HttpPost postMethod = new HttpPost("https://login.salesforce.com/services/oauth2/token");
        List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
        params.add(new BasicNameValuePair("client_id", MY_CLIENT_ID));
        params.add(new BasicNameValuePair("client_secret", MY_SECRET));
        params.add(new BasicNameValuePair("grant_type", "password"));
        params.add(new BasicNameValuePair("username", MY_USERNAME));
        params.add(new BasicNameValuePair("password", MY_PASSWORD + SECURITY_TOKEN));

        try
        {
            postMethod.setEntity(new UrlEncodedFormEntity(params, Consts.UTF_8));
            HttpResponse response = httpclient.execute(postMethod);
            InputStream content = response.getEntity().getContent();

            JSONObject authResponse = new JSONObject(new JSONTokener(new InputStreamReader(content)));
            System.out.println("Auth response: " + authResponse.toString(2));

            String accessToken = authResponse.getString("access_token"); // throws exception JSONObject["access_token"] not found.

            System.out.println("Got access token: " + accessToken);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

The output ot the code snippet:

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

 

Kind regards.