我使用的是Dropbox API for Java 1.6版本:https://www.dropbox.com/developers/core/sdks/java
我也在Eclipse 3.7中使用GWT 2.5.1
我有以下代码,当作为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正在做一些事情,它根据可用的类动态加载连接。但我无法弄明白。
提前感谢您的帮助!
编辑:
好的,所以我查看了Dropbox API源代码,错误发生在这里:
URL urlObject = new URL(url);
HttpsURLConnection conn = (HttpsURLConnection) urlObject.openConnection(this.proxy);
因为我使用的是Google App Engine,所以它使用的是自己的URL对象,而不是App Engine API导入的对象。关于解决方案的任何想法都不涉及为Dropbox API编写GWT包装器。
答案 0 :(得分:8)
最新的Dropbox SDK允许您选择HttpRequestor实现
new DbxRequestConfig(APP_NAME, userLocale, HttpRequestor);
所以你需要做的就是使com.dropbox.core.http.StandardHttpRequestor适应Appengine友好
答案 1 :(得分:1)
您是否使用安全的https://
网址进行连接?我的猜测是,如果您使用http://
,则会获得无法转换为安全连接的不安全连接器。
<强> - 编辑 - 强>
看起来GAE上根本不支持HttpsURLConnection
类,因此它不会像GAE文档所说的那样起作用。 Using HttpsURLConnection (doc issue)。这意味着你可能不会直接使用它。
答案 2 :(得分: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
中使用此请求程序,它对我有效。
答案 3 :(得分: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
因此我取代了班级
javax.net.ssl.HttpsURLConnection
通过
java.net.HttpURLConnection
在com.dropbox.core.http.StandardHttpRequestor类中重建Dropbox Java SDK。它的工作正常。示例工作应用可以是https://gwt-gae-testing.appspot.com/
答案 4 :(得分: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说。它工作正常。