繁体   English   中英

具有GWT身份验证问题的Dropbox Java API

[英]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友好

要点: AppengineHttpRequestor.java

您是否使用安全的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使用此请求程序, DbxRequestConfigDbxRequestConfig

我也遇到了同样的问题。 根据这篇文章, 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM