[英]Dropbox Java API with GWT Authentication problems
我在這里使用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正在做一些事情,它根據可用的類動態加載連接。 但我無法弄明白。
先謝謝您的幫助!
EDITS:
好的,所以我查看了Dropbox API源代碼,錯誤發生在這里:
URL urlObject = new URL(url);
HttpsURLConnection conn = (HttpsURLConnection) urlObject.openConnection(this.proxy);
因為我使用的是Google App Engine,所以它使用的是自己的URL對象,而不是App Engine API導入的對象。 關於解決方案的任何想法都不涉及為Dropbox API編寫GWT包裝器。
最新的Dropbox SDK允許您選擇HttpRequestor實現
new DbxRequestConfig(APP_NAME, userLocale, HttpRequestor);
所以你需要做的就是使com.dropbox.core.http.StandardHttpRequestor適應Appengine友好
您是否使用安全的https://
url進行連接? 我的猜測是,如果您使用http://
您將獲得無法轉換為安全連接的不安全連接器。
- 編輯 -
它看起來在GAE上根本不支持HttpsURLConnection
類,因此它不會像GAE文檔那樣工作。 使用HttpsURLConnection(doc issue) 。 這意味着你可能不會直接使用它。
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);
因此,通過在StandardHttpRequestor
使用Proxy
(我的防火牆)並在DbxRequestConfig
使用此請求程序, DbxRequestConfig
我DbxRequestConfig
。
我也遇到了同樣的問題。 根據這篇文章, 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/
這是檢索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說。 它工作正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.