2017-10-15 270 views
0

我想使用Jsoup登录到Twitter,因为我想刮我的推文,并通过短信发送给我,我知道如何处理它们从HTML,我知道如何通过发送它们通过短信,我需要帮助登录步骤,我无法登录到twitter 我试过的是,在后台处理webview(没有任何用户界面)(注意:没有应用程序的用户界面),但失败了,因为它可以“T在不UI处理,与Jsoup我尝试使用此代码推特通过Jsoup Java登录Android

final String usernameKey = "session[username_or_email]"; 
final String passwordKey = "session[password]"; 
final String loginFormUrl= "https://mobile.twitter.com/login"; 
final String loginActionUrl = "https://mobile.twitter.com/sessions"; 

HashMap<String, String> cookies = new HashMap<>(); 
HashMap<String, String> formData = new HashMap<>(); 

Connection.Response loginForm = Jsoup.connect(loginFormUrl) 
        .userAgent(userAgent) 
        .method(Connection.Method.GET) 
        .execute(); 

cookies.putAll(loginForm.cookies()); 

doc = Jsoup.connect(loginActionUrl) 
    .data(usernameKey, username) 
    .data(passwordKey, password) 
    .cookies(cookies) 
    .method(Connection.Method.POST) 
    .userAgent(userAgent) 
    .post(); 

       /* 
       doc = Jsoup.connect(twitterHomePage) 
       .userAgent(userAgent) 
       .cookies(loggedIn.cookies()) 
       .timeout(30 * 1000) 
       .get(); 
       */ 
Log.d(TAG, doc.html()); 

登录但我没有登录,只有获得Twitter的登录页面HTML

+0

是否有任何理由,你为什么不想使用Twitter API做这件工作效率LY? – codeFreak

+0

因为我认为它会让我的应用程序的大小保持不变,但Twitter屡次阻止我使用jsoup一次又一次地登录到我的帐户,现在,我必须使用twitter API,请给我一个链接如何使用twitter API API? –

回答

1

注:Twitter的只有2次登录此方法后才阻止我的帐户,请自行承担风险。

这是我如何得到它的工作

Map<String, String> cookies; 
Map<String, String> data = new HashMap<String, String>(); 

Connection.Response loginPageRes = Jsoup.connect(loginFormUrl) 
    .userAgent(userAgent) 
    .referrer(refferer) 
    .timeout(30 * 1000) 
    .method(Connection.Method.GET) 
    .followRedirects(true) 
    .execute(); 

cookies = loginPageRes.cookies(); 

data.put(usernameKey, username); 
data.put(passwordKey, password); 
data.put("remember_me", "1"); 
data.put("wfa", "1"); 
data.put("redirect_after_login", "/"); 
data.put("commit", " Log in "); 
data.put("authenticity_token", loginPageRes.parse().select("input[name=authenticity_token]").val()); 

Connection.Response resPostLogin = Jsoup.connect(loginActionUrl) 
    .method(Connection.Method.POST) 
    .userAgent(userAgent) 
    .referrer(loginFormUrl) 
    .data(data) 
    .cookies(cookies) 
    .timeout(30 * 1000) 
    .followRedirects(true) 
    .execute(); 

doc = resPostLogin.parse(); 
    String timelineText = doc.text(); 
1

我不希望从Twitter的API页面复制代码推倒重来,所以我将只是链接的如何,为登录https://dev.twitter.com/twitterkit/android/log-in-with-twitter

对于阅读鸣叫和其他功能:https://dev.twitter.com/twitterkit/android/access-rest-api

+0

我不是一个专业开发人员,所以我没有得到它,如何使用这个API与我的用户界面较少的应用程序 –

+0

你不需要成为一个“专业开发人员”使用API​​。仅供参考,它可以在没有UI的情况下工作。这是解决您的问题的推荐方式。 – codeFreak