Search code examples
javaauthenticationproxydropboxdropbox-api

Dropbox Java: Use Proxy with authentication


I want to create my DbxRequestConfig Object with a StandardHttpRequestor, because I need it to use a Proxy.

The Proxy is a http Proxy, Port 80, and needs authentication.

Proxyaddress:    http://myproxy.com
Proxyport:       80
Proxyusername:   username
Proxypassword:   password

So I tried to use the global Java Proxy setup:

System.setProperty("http.proxy","proxyaddress") //... http.proxyUser, http.ProxyPassword
//and so on

It did not work.

After looking into the StandardHttpRequestor I realized I need to use this Object as well as a Proyx Object:

Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress(ip,port));            
StandardHttpRequestor requ = new StandardHttpRequestor(proxy);

Which is wrong, because it has no authentication.

For authentication, the net and google show me the following. Putting all together, my current code looks like the following:

String ip = "http://myproxy.com";
int port = 80;

final String authUser = "username";
final String authPassword = "password";

Authenticator.setDefault(new Authenticator() {
    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
         return new PasswordAuthentication(authUser, authPassword.toCharArray());
    }
});

System.setProperty("http.proxyUser", authUser);
System.setProperty("http.proxyPassword", authPassword);

Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress(ip,port));
StandardHttpRequestor requ = new StandardHttpRequestor(proxy);
return requ;

But this does not work as well.

What am I doing wrong? I can't seem to get the Proxy to work.


Solution

  • One problem was the http:// in String ip = "http://myproxy.com";

    My current code looks the following, and works sometimes. Sometimes not. I have no idea why. Sometimes I have to reallow the App to be connected to my DropBox Account, because the authKey doesn't come through the proxy...

    Well at least I got an example working for you guys having the same trouble. Maybe the rest of the problem is on the proxy side? I'll have a further look into this. But here comes my code:

    public HttpRequestor getProxy(){
    
        if("true".equals(config.getProperty("proxy","false"))){
            String ip = "proxy.myproxy.com";
            int port = 80;
    
            final String authUser = "username";
            final String authPassword = "password";
    
            Authenticator.setDefault(new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(authUser, authPassword.toCharArray());
                }
            });
    
            Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress(ip,port));
    
    
            HttpRequestor req = new StandardHttpRequestor(proxy);
            return req;
        }
        return null;
    }
    

    As you can see I don't use the StandardHttpRequestor anymore. For the Dropbox code it is the following:

    HttpRequestor requ = con.getProxy();
    if(requ!=null)
        config = new DbxRequestConfig(APP_NAME, Locale.getDefault().toString(),requ);
    else
        config = new DbxRequestConfig(APP_NAME, Locale.getDefault().toString());
    

    As I already said, sometimes it works. Sometimes not. I'm going to write more info about that as soon as I know if it's because of the code or because of the proxy itself.