简体   繁体   English

如何使用Android作为客户端访问express.js / passport.js API?

[英]How can I access express.js/passport.js API using Android as client?

I'm trying to access an express.js api done with node.js and passport.js. 我正在尝试访问用node.js和passport.js完成的express.js api。 Some methods just work when req.use is available. 当req.use可用时,某些方法才起作用。

I try to log with facebook and twitter from my client app, and I get plain cookie. 我尝试从我的客户端应用程序登录Facebook和Twitter,但我得到了纯Cookie。

Something like connect.sid=jkbsdkjabdkjasdbjkadnaidiadiasdaoidoa 类似于connect.sid = jkbsdkjabdkjasdbjkadnaidiadiasdaoidoa

I recreate the client cookie with this method: 我使用以下方法重新创建客户端cookie:

            BasicClientCookie clientCookie = new                BasicClientCookie(Metadata.COOKIE_LOGIN, cookieValue);
    clientCookie.setDomain("www.domain.com");
    clientCookie.setPath("/");
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DAY_OF_MONTH, 1);
    clientCookie.setExpiryDate(calendar.getTime());
    return clientCookie;

And after that I make Json Requests with this: 之后,我用以下命令发出Json Requests:

    HttpUriRequest request = method.createRequest(uri);
    request.addHeader("Accept", "application/json");
    HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
    DefaultHttpClient client = new DefaultHttpClient(params);
    client.getCookieStore().addCookie(USER_SESSION_COOKIE);
    HttpResponse response = client.execute(request);
    String serializedJson = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
    T fromJson = gsonParser.fromJson(serializedJson, clazz);

I cannot get on express' routes methods the user as usual. 我无法像往常一样使用快速路线方法。

I know that I'm doing something wrong but I don't know what. 我知道我做错了,但我不知道该怎么办。

Anyone has done a connection betweem an android app and passport.js? 有人在android应用程序和Passport.js之间建立了连接吗?

Thank you all! 谢谢你们!

See this question for a quick insight on how to use OkHttp with a persistent cookie store. 请参阅此问题 ,以快速了解如何将OkHttp与持久性cookie存储一起使用。 Using a HTTP library to access your REST API will make things much easier. 使用HTTP库访问REST API将使事情变得简单得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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