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
indyindy 

OAuth Integration ....Urgent Please.

Hi All,

I am writing the follwoing standard alone java code to connect salesforce. My intention is to get data from oracle data base and update sdfc data. Using OAuth authentication.

 

import java.io.IOException;
import java.net.CookiePolicy;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;

import com.sforce.ws.ConnectorConfig;

/*
Java client application to connect to IW database.
This client application uses Oauth as a Authentication mechanism.
*/
public class AF_IW_Integration 
{

private static final String redirectURL = "resttest:callback";
public static void main(String[] args) throws IOException
{
System.setProperty("proxyHost","xxxxxx");
System.setProperty("proxyPort","xxxxxxx");
HttpPost post = new HttpPost("https://cs11.salesforce.com/services/oauth2/token");

HttpClient client = new DefaultHttpClient();

List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("client_id","xxxxxx"));
params.add(new BasicNameValuePair("client_secret","xxxxxxxx"));
params.add(new BasicNameValuePair("username","usernam"));
params.add(new BasicNameValuePair("password","pass+token"));
//params.add(new BasicNameValuePair("redirect_uri",redirectURL));
params.add(new BasicNameValuePair("grant_type","password"));

post.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
ResponseHandler<String> responseHandler = new BasicResponseHandler();

HttpEntity httpEntity = null;
System.out.println("Post Base URL***"+post);
try
{

String response = client.execute(post,responseHandler);
System.out.println("Http Response***"+response);
}
finally
{

}
}
}

 

 

When I try to execute the below code...I am getting the error message....

==============================================================================================

Exception in thread "main" org.apache.http.conn.HttpHostConnectException: Connection to https://cs11.salesforce.com refused
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:190)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:294)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:643)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:479)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:1138)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:1076)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:1050)
at AF_IW_Integration.main(AF_IW_Integration.java:56)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(Unknown Source)
at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:414)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180)
... 8 more

===============================================================================================

 

The proxy settings are correct. Also I can able to run the below code without any issues.

 

public static void main(String[] args) {

ConnectorConfig config = new ConnectorConfig();
config.setUsername(USERNAME);
config.setPassword(PASSWORD);
config.setProxy("proxy.cognizant.com", 6050);
config.setAuthEndpoint("https://login.salesforce.com/services/Soap/c/26.0");
try {

// connect to the salesforce instance
connection = Connector.newConnection(config);

// display some current settings
System.out.println("Auth EndPoint: " + config.getAuthEndpoint());
System.out.println("Service EndPoint: " + config.getServiceEndpoint());
System.out.println("Username: " + config.getUsername());
System.out.println("SessionId: " +config.getSessionId());

}

 

Any help is greatly appreciated.

 

 

Thanks,

Indy.

HeliusHelius

Since you're doing https://cs11.salesforce.com/, check the simple stuff first -

 

Can you ping that hostname? telnet it on port 80? 443? What about login.salesforce.com instead?

 

Let me know the results.

indyindy

Hi Helius,

Thanks for your reply.

How to ping the hostname? could you please eloborate.

 

Thanks,

Indy

HeliusHelius

I presume you're working from a server. That server should have a command line. You will want to run the ping test from command line. Google how to do it if you need to. It's simple - "ping hostname.TLD" and the output will show the speed of each packet sent.