2

I want to upload files using the java API for DropBox. The following code gets me the oauth_token and oauth_secret. but when ever I try to upload a file I get a exception.
Java Class

package com.dropbox.client;

import com.dropbox.client.DropboxAPI.Config;
import java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Charan
 */
public class DBmain {

    public DBmain() {
        System.setProperty("java.net.useSystemProxies", "true");
        Map configuration= new HashMap();
        configuration.put("consumer_key", "XXXXXXXXXXXXXXXX");
        configuration.put("consumer_secret", "XXXXXXXXXXXXXXXX");
        configuration.put("request_token_url", "http://api.dropbox.com/0/oauth/request_token");
        configuration.put("access_token_url", "http://api.dropbox.com/0/oauth/access_token");
        configuration.put("authorization_url", "http://api.dropbox.com/0/oauth/authorize");
        configuration.put("port",80);
        //configuration.put("trusted_access_token_url","http://api.getdropbox.com/0/token");
        configuration.put("server","api.getdropbox.com");
        configuration.put("content_server","api-content.dropbox.com");
        String username="myMailAddress@domain.com";
        String password="myPassword";
        try {
           Authenticator auth = new Authenticator(configuration);
           String url = auth.retrieveRequestToken("");
           String access_key = auth.getTokenKey();
           String access_secret = auth.getTokenSecret();
           System.out.println(access_key);
           System.out.println(access_secret);

           DropboxAPI api = new DropboxAPI();
           DropboxAPI.Config conf = api.new Config(configuration);
           api.authenticateToken("XXXXXXXXXXXX", "XXXXXXXXXXX", conf);
           System.out.println(api.isAuthenticated());
           URL resource = this.getClass().getResource("/config/testing.json");
            
           File f= new File(resource.toURI());
           api.putFile("dropbox", "/Project", f);

           //api.accountInfo(); //even this method gives me a exception
        } catch (Exception e) {
                e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
            new DBmain();
        }
}

Exception

Exception in thread "main" java.lang.NoSuchMethodError: org.apache.http.protocol.BasicHttpContext: method <init>()V not found
    at org.apache.http.impl.client.DefaultHttpClient.createHttpContext(DefaultHttpClient.java:205)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:532)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
    at com.dropbox.client.DropboxClient.putFile(DropboxClient.java:299)
    at com.dropbox.client.DropboxAPI.putFile(DropboxAPI.java:463)
    at com.dropbox.client.DBmain.<init>(DBmain.java:58)
    at com.dropbox.client.DBmain.main(DBmain.java:70)
Java Result: 1

Some extra info

Edit Date:18-6-2011

I changed the httpclient-4.0-beta1.jar and httpcore-4.0-alpha6.jar to httpclient-4.0.jar and httpcore-4.0.1.jar respectively and I no longer get the above exception [ java.lang.NoSuchMethodError ] This stackoverflow question helped me in solving this : java.lang.NoSuchMethodError: org.apache.http.protocol.BasicHttpContext: method <init>()V not found

But Now I get UnknownhostException on execution of any methods of the API

com.dropbox.client.DropboxException: java.net.UnknownHostException: api.getdropbox.com:80
    at com.dropbox.client.RESTUtility.request(RESTUtility.java:250)
    at dump.DropboxClient.accountInfo(DropboxClient.java:121)
    at com.charan.client.DBmain.<init>(DBmain.java:57)
    at com.charan.client.DBmain.main(DBmain.java:65)
Caused by: java.net.UnknownHostException: api.getdropbox.com:80
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436)
    at java.net.Socket.connect(Socket.java:525)
    at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:123)
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:123)
    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:147)
    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:101)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:381)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:641)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:576)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:554)
    at com.dropbox.client.RESTUtility.executeRequest(RESTUtility.java:172)
    at com.dropbox.client.RESTUtility.request(RESTUtility.java:248)
    ... 3 more
Community
  • 1
  • 1
Charanraj Golla
  • 1,102
  • 3
  • 17
  • 35
  • 1
    You should do the following: Post an answer to your question with the information you added above that helped you resolve the original question. Then create a _new_ question about the UnknownHostException. – Jim Garrison Jun 20 '11 at 17:17
  • BTW, api.getdropbox.com resolves to 174.36.30.69 for me. What happens if you try pinging the hostname from the command line? – Jim Garrison Jun 20 '11 at 17:17
  • @Jim I tried using the ping command but every time I use it with any host I get the same error `Ping request could not find host google.com. Please check the name and try again.` I'm using my cellphone as a modem with proxy. Well I'm able to surf the web but the ping command doesn't work at all. Root cause of my unknownhostException . – Charanraj Golla Jun 21 '11 at 03:14
  • 1
    @charanraj If you're still struggling with this, I've thrown up an example here that may help: http://berry120.blogspot.com/2012/02/dropbox-java-api.html – Michael Berry Feb 18 '12 at 13:27
  • 1
    @berry120 Thanks for sharing the link.I'll check it out – Charanraj Golla Feb 22 '12 at 08:15
  • Try including the jars mentioned here in your classpath. http://www.jarfinder.com/index.php/java/info/org.apache.http.protocol.BasicHttpContext – Jacob Alex Sep 14 '12 at 04:01

3 Answers3

2

You should use http-client 4.0.3 jar

DannyGolhar
  • 196
  • 1
  • 10
1

Your class should NOT be in package com.dropbox.client. Try moving it to a different (i.e. com.yourname.client). It looks like you might be creating a name clash and inadvertently overriding something in the Dropbox client API.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
0

You have specified api.getdropbox.com:80 as the host name. Try using api.getdropbox.com instead.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347