Search code examples
javagwtdropboxdropbox-api

Dropbox Java API with GWT Authentication problems


I am using version 1.6 of the Dropbox API for Java found here: https://www.dropbox.com/developers/core/sdks/java

I am also using GWT 2.5.1 in Eclipse 3.7

I have the following code which works when run as a Java Applcation:

    DbxRequestConfig requestConfig = new DbxRequestConfig(type, locale);
    DbxAppInfo appInfo = new DbxAppInfo(APP_ID, APP_SECRET);
    DbxWebAuthNoRedirect webauth = new DbxWebAuthNoRedirect(requestConfig, appInfo);
    String result = webauth.start();
    System.out.println(result);
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    String code = reader.readLine();

    webauth = new DbxWebAuthNoRedirect(requestConfig, appInfo);
    DbxAuthFinish finish = webauth.finish(code);

    DbxClient client = new DbxClient(requestConfig, finish.accessToken);
    DbxAccountInfo info = client.getAccountInfo();
    long total = info.quota.total;
    long used = info.quota.normal;

    System.out.println("total: " + total);
    System.out.println("used: " + used);

This works just fin when I run it as a Java Application. However, when I try to do something similar with GWT within a RemoteServiceServlet. I get an exception when I try to do

webauth = new DbxWebAuthNoRedirect(requestConfig, appInfo);

The exception I am getting is the following:

Caused by: java.lang.ClassCastException: com.google.apphosting.utils.security.urlfetch.URLFetchServiceStreamHandler$Connection cannot be cast to javax.net.ssl.HttpsURLConnection
at com.dropbox.core.http.StandardHttpRequestor.prepRequest(StandardHttpRequestor.java:160)
at com.dropbox.core.http.StandardHttpRequestor.startPost(StandardHttpRequestor.java:87)
at com.dropbox.core.http.StandardHttpRequestor.startPost(StandardHttpRequestor.java:21)
at com.dropbox.core.DbxRequestUtil.startPostNoAuth(DbxRequestUtil.java:156)
at com.dropbox.core.DbxRequestUtil.doPostNoAuth(DbxRequestUtil.java:289)
at com.dropbox.core.DbxWebAuthHelper.finish(DbxWebAuthHelper.java:40)
at com.dropbox.core.DbxWebAuthNoRedirect.finish(DbxWebAuthNoRedirect.java:84)
at com.cloudshare.server.DropboxPlayground.getFinish(DropboxPlayground.java:21)
at com.cloudshare.server.DropboxServiceImpl.authenticate(DropboxServiceImpl.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.google.appengine.tools.development.agent.runtime.Runtime.invoke(Runtime.java:115)
at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:561)
... 40 more

I have been banging my head against a wall for last couple of hours trying to figure out what is going on. I originally wanted to use a DbxWebAuth but the documentation in their API includes instructions that have classes that don't exist (I am assuming they did at one time).

I feel like the DbxWebAuthNoRedirect is doing something where it is dynamically loading a connection based on the classes that are available. But I have not been able to figure it out.

Thanks in advance for the help!

EDITS:

Okay, so I looked at the Dropbox API source and the error is occurring here:

    URL urlObject = new URL(url);
    HttpsURLConnection conn = (HttpsURLConnection) urlObject.openConnection(this.proxy);

Because I am using Google App Engine, it is using its own URL object rather than the one imported by the App Engine API. Any ideas on a solution that doesn't involve writting a GWT wrapper for the Dropbox API.


Solution

  • Did you use a secure https:// url to connect? My guess is if you use a http:// you get the unsecured connector that can't be cast to a secure connection.

    --edit--

    It looks the HttpsURLConnection class is simply not supported on GAE, therefor it will not work unlike the GAE documentation says. Using HttpsURLConnection (doc issue). This means you're probably out of luke using it directly.