• Stevan Buzejic
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

Hi,

 

Here is another similar situation, but this time I tried to request for access token with refresh_token in Java. I got error "java.io.IOException: Server returned HTTP response code: 400 for URL: https://login.salesforce.com/services/oauth2/token". It works if I use a self-submitted html form to get access token from Salesforce.

 

Here is the Java codes for the request:

                String tokenUrl = "https://login.salesforce.com/services/oauth2/token";
		HttpURLConnection httpConnection = null;
		DataOutputStream dos = null;
		
		try{
			StringBuilder sb = new StringBuilder("grant_type=refresh_token");
			sb.append("&client_id=").append("3MVG9Km_cBLhsuPy92UAudTyfc9ka4dxuV.OMS5r0vyIS_ThDWpVFBpenmEEUGIHUrBCeHC28ZqbFDWkcCUAd");
			sb.append("&client_secret=").append("7408566340993550860");
			sb.append("&refresh_token=").append("5Aep861eWO5D.7wJBtkhcv3GwrwrKskc75_Jdh9vHLiDWD3xma5eXdnTYq5PANplHLfLtw.ri7UUA==");
			
			// open the connection for mutli-part POST for the first chunk
	                HttpURLConnection httpConnection = (HttpURLConnection) new URL (tokenUrl).openConnection ();
                        httpConnection.setRequestMethod ("POST");
		        httpConnection.setConnectTimeout (300000); //300s = 5mins
		        httpConnection.setReadTimeout (300000); //300s = 5mins
		
		        httpConnection.setDoInput (true);
		        httpConnection.setDoOutput (true);
	                httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
	                httpConnection.setRequestProperty("Accept", "*/*");
	        
	                dos = new DataOutputStream(httpConnection.getOutputStream());
	                dos.writeBytes(sb.toString());
	                dos.flush();
	        
			StringBuffer responseBuffer = StreamUtils.gatherStream(httpConnection.getInputStream());
			System.out.println("SFDC replied access token: " + responseBuffer.toString());
	        
		} catch (Exception e) {
			String msg = "Failed to get access token when executing....";
		} finally {
            if (httpConnection != null) {
                httpConnection.disconnect();
            }

            if (dos != null) {
                // close the stream
                try {
		    dos.close();
		} catch (IOException e) {
		    // NOTHING TO DO
		}
            }
        }

Also i really appreciate it if you can help me this out.

 

Thanks,

Michael