1
votes

I'm new to programming and I'm having some difficulty with basic authentication on Apache Olingo.

The Problem is I'm trying to access an ODATA with basic authentication. When I tried to access the link using my credentials, it was working fine. Unfortunately when I tried to eclipse, I'm getting an error message; "Hostname certificate did not match".

Here's my Code. Hope you could help me.

String serviceRoot = "";// URL that I'm trying to access
    URI ExerciseUri = new URI(serviceRoot);
    ODataClient client = ODataClientFactory.getV4();
    client.getConfiguration().setHttpClientFactory(new BasicAuthHttpClientFactory("USERNAME", "PASSWORD"));

    ODataRetrieveResponse<ODataEntity> responseExercise = client.getRetrieveRequestFactory()
            .getEntityRequest(ExerciseUri).execute();

PS: I already check this site and found solution but still not working. Also, I'm using Java.

2
Is there a way to use oauth2 for authentication? stackoverflow.com/questions/69390280/…DOS

2 Answers

1
votes

Olingo client library uses Apache HttpComponents to call out to Dynamics. The error message indicates that SSL certificate is invalid during handshake between client and server. You may want to check Java's SSL store setup. Posting some stacktrace of exception would be useful for people to dig deeper into the problem.

1
votes

Try with the below code...

CustomeBasicAuthHttpClientFactory Customfact =
              new CustomeBasicAuthHttpClientFactory(username, password);
client.getConfiguration().setHttpClientFactory(Customfact);
import java.util.ArrayList;

import javax.net.ssl.SSLContext;

import org.apache.http.*;
import org.apache.olingo.client.core.http.DefaultHttpClientFactory;
import org.apache.olingo.commons.api.http.HttpMethod;
public class CustomeBasicAuthHttpClientFactory extends DefaultHttpClientFactory {

      private final String username;
      private final String password;
      public CustomeBasicAuthHttpClientFactory(String username, String password) {
        this.username = username;
        this.password = password;
      }

      public DefaultHttpClient create(HttpMethod method, URI uri) {
          Logger.logMessage(LogLevel.LEVEL_DEBUG, ODATAstepAdaptor.ODATA_CLIENT_LOG_MODULE, "In side:create(HttpMethod method, URI uri)");
          DefaultHttpClient client=null;
          String[] tlsProtocols=null;
          String[] cipherSuites=null;
          try { 
                String filepath=System.getProperty("com.xxx.xxxx.home");
                Logger.logMessage(LogLevel.LEVEL_DEBUG, ODATAstepAdaptor.ODATA_CLIENT_LOG_MODULE, "FilePath=>"+filepath+"/java/conf/tlsProtocols.dat");
                BufferedReader in = new BufferedReader(new FileReader(filepath+"/java/conf/tlsProtocols.dat"));
                String str=null;
                ArrayList<String> lines = new ArrayList<String>();
                while((str = in.readLine()) != null){
                    lines.add(str);
                }
                in.close();
                tlsProtocols = lines.toArray(new String[lines.size()]);
               SSLContext sslContext = SSLContext.getInstance("TLS");
               sslContext.init(null, null, null);
               //SSLSocketFactory sf = new SSLSocketFactory(sslContext); 
               SSLSocketFactory sf = new CustomizedSSLSocketFactory(sslContext,null,tlsProtocols,cipherSuites);
               sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 

               HttpParams params = new BasicHttpParams(); 
               HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 
               HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); 

               SchemeRegistry registry = new SchemeRegistry(); 
               registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
               registry.register(new Scheme("https", sf, 443));
               ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

               client=new DefaultHttpClient(ccm, params);
               client.getCredentialsProvider().setCredentials(new AuthScope(uri.getHost(), uri.getPort()), new UsernamePasswordCredentials(this.username, this.password));
           } catch (Exception e) { 
               Logger.logMessage(LogLevel.LEVEL_ERROR, ODATAstepAdaptor.ODATA_CLIENT_LOG_MODULE,e.getMessage());
           } 
          return client;
      }

}