2013-07-25 103 views
2

我使用Dropbox的API的Java的1.6版本在这里找到:https://www.dropbox.com/developers/core/sdks/java的Dropbox的Java API

我也使用GWT 2.5.1在Eclipse 3.7

我有以下代码作为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); 

这只适用于当我运行它作为Java应用程序。但是,当我尝试在RemoteServiceServlet中使用GWT进行类似的操作时。我得到一个异常时,我尝试做

webauth = new DbxWebAuthNoRedirect(requestConfig, appInfo); 

我得到例外的是以下内容:

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 

我一直在敲打我的头靠在墙上的最后几个小时,试图找出到底是怎么回事。我原本想使用DbxWebAuth,但其API中的文档包含有不存在的类的指令(我假设他们曾经一次)。

我觉得DbxWebAuthNoRedirect正在根据可用的类动态加载连接。但我一直无法弄清楚。

在此先感谢您的帮助!

EDITS:

好了,我看着的Dropbox API源和错误发生在这里:

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

因为我使用谷歌应用程序引擎,它使用自己的URL对象,而比App Engine API导入的版本更高。任何关于不涉及为Dropbox API写入GWT包装的解决方案的想法。

+1

您是在为Google应用引擎开发吗?如果是这样,在Dropbox API所使用的错误消息“HttpsURLConnection”中提到的类与应用程序引擎的工作方式不兼容。如果您没有开发应用程序引擎,则需要禁用应用程序引擎。 –

+0

我正在使用GAE。任何关于解决问题的想法? – Joe

+0

在你的编辑。 GAE根据传递的URL在内部创建一个连接器。但是,由于dropbox api是一个'https' url,因此dropbox api希望返回一个安全的连接类,并将它转换为该类。但GAE只支持'http',并且会返回一个不安全的http连接器,该连接器不能转换为安全连接类。由于dropbox api的源代码在github上,因此您可以尝试将代码更改为不安全的对象,但我不知道这是否可行。 (作为一个方面说明,这不是一个GWT问题,而是一个GAE问题) –

回答

1

您是否使用安全的https://网址进行连接?我的猜测是,如果你使用http://,你会得到不安全的连接器,无法连接到安全连接。

- 编辑 -

它看起来HttpsURLConnection类是根本不支持GAE,为此它不会工作不像GAE文件说。 Using HttpsURLConnection (doc issue)。这意味着你可能没有直接使用它。

+0

不幸的是,我没有给出这种请求类型的选项,因为我正在使用它们的API。我只需在'DbxWebAuthNoRedirect'的实例上调用finish方法,该方法可能使用http而不是https。但是,我对此持怀疑态度,因为它生成一个https站点来访问身份验证。也许GWT生成一个http请求,而不是https?这会令人惊讶,因为我在使用GWT实施Google Drive API时没有问题。 – Joe

+0

看起来Dropbox源代码使用的是GAE中更改的Java URL类。我想我可能会修改Dropbox来解决这个问题,但我不确定。有任何想法吗? – Joe

+0

从Dropbox SDK for Java 2.0.5版开始,您可以使用[GoogleAppEngineHttpRequestor](https://dropbox.github.io/dropbox-sdk-java/api-docs/v2.0.x/com/dropbox /core/http/GoogleAppEngineRequestor.html),它明确支持App Engine的网址提取API。 – Greg

0

我也面临同样的问题。按照这个帖子,https://groups.google.com/forum/#!topic/google-appengine-java/V8pREOXPX24

com.google.apphosting.utils.security.urlfetch.URLFetchServiceStreamHandler 
$Connection extends the class java.net.HttpURLConnection 

因此,我在课堂上com.dropbox.core.http.StandardHttpRequestor取代了阶级

javax.net.ssl.HttpsURLConnection 

通过

java.net.HttpURLConnection 

和重建的Dropbox的Java SDK。它的工作正常。样本工作程序可在https://gwt-gae-testing.appspot.com/

8

最新的Dropbox SDK可以让你选择一个HttpRequestor实现

new DbxRequestConfig(APP_NAME, userLocale, HttpRequestor);

因此,所有你需要做的是适应com.dropbox.core.http.StandardHttpRequestor是AppEngine上友好

要点:AppengineHttpRequestor.java

+0

它适合我。非常感谢!你救了我的命:-P – Aerox

1
DbxAppInfo appInfo = new DbxAppInfo(APP_KEY, APP_SECRET); 
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("107.108.85.10", 80)); 

StandardHttpRequestor requ = new StandardHttpRequestor(proxy); 
DbxRequestConfig config = new DbxRequestConfig("JavaTutorial/1.0", 
Locale.getDefault().toString(),requ); 
DbxWebAuthNoRedirect webAuth = new DbxWebAuthNoRedirect(config, appInfo); 

因此,通过使用Proxy(我的防火墙)在StandardHttpRequestor和使用这个请求者在DbxRequestConfig它为我工作。

0

这是检索access_token的唯一方法(但我不知道如何处理Dropbox API的其他方法)。

@Override 
protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
     throws ServletException, IOException { 

    String code = req.getParameter("code"); 

    URL fetchurl = new URL(url); 
    HTTPRequest request = new HTTPRequest(fetchurl, HTTPMethod.POST); 
    String body = "code=" + code + "&grant_type=authorization_code" + "&client_id=" + dropboKey + "&client_secret=" + dropboxSecret + "&redirect_uri=" + redirectUri; 
    request.setPayload(body.getBytes("UTF-8")); 

    HTTPResponse response = URLFetchServiceFactory.getURLFetchService().fetch(request); 

    String respInJson = new String(response.getContent()); 

    LOGGER.warning(respInJson); 

    JSONObject jsonObj; 
    try { 
     jsonObj = new JSONObject(respInJson); 
     String uid=jsonObj.getString("uid"); 
     String access_token=jsonObj.getString("access_token"); 

    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
} 

我建议改变HttpRequestor为@ radu-c说。它工作正常。