【问题标题】:How to use the twitter4j lib to get the tweets of screen name?如何使用 twitter4j 库获取网名的推文?
【发布时间】:2013-05-15 21:31:09
【问题描述】:

我看过很多关于使用这个库的教程,但我对它有一个清晰的概念。

首先我如何验证 twitter 应用程序??,

有什么方法可以硬编码访问令牌,这样用户就不必做任何事情,他可以通过输入屏幕名称直接搜索特定用户的推文??

提及网名后如何获取推文??

我尝试使用 twitter4j lib 阅读文档,但它对我有帮助....

我需要帮助,我被困在这两天了,请帮助...

【问题讨论】:

    标签: android twitter twitter4j tweets


    【解决方案1】:

    有多种认证方式:

    首先您需要创建一个应用程序here。 然后,您将收到您的使用者密钥和秘密:

    然后您可以使用此代码在启动时请求授权。

    public class MainActivity extends Activity {
    
        // TwitterProperties
        private CommonsHttpOAuthConsumer httpOauthConsumer;
        private OAuthProvider httpOauthprovider;
    
        public final static String consumerKey = "YOUR CONSUMER KEY";
        public final static String consumerSecret = "YOUR CONSUMER SECRET";
    
        private final String CALLBACKURL = "SCHEME://HOST";
    
        private Twitter twitter;
        AccessToken a;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            StrictMode.enableDefaults();
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
            setContentView(R.layout.activity_main);
    
            doAuth();
        }
    
        private void doAuth() {
            try {
                httpOauthConsumer = new CommonsHttpOAuthConsumer(consumerKey,
                        consumerSecret);
                httpOauthprovider = new DefaultOAuthProvider(
                        "https://twitter.com/oauth/request_token",
                        "https://twitter.com/oauth/access_token",
                        "https://twitter.com/oauth/authorize");
                String authUrl = httpOauthprovider.retrieveRequestToken(
                        httpOauthConsumer, CALLBACKURL);
    
                this.startActivity(new Intent(Intent.ACTION_VIEW, Uri
                        .parse(authUrl)));
            } catch (Exception e) {
                Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
            }
        }
    
    
        @Override
        protected void onNewIntent(Intent intent) {
            super.onNewIntent(intent);
    
            Uri uri = intent.getData();
            if (uri != null && uri.toString().startsWith(CALLBACKURL)) {
    
                String verifier = uri
                        .getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
    
                // this will populate token and token_secret in consumer
                try {
                    httpOauthprovider.retrieveAccessToken(httpOauthConsumer,
                            verifier);
                } catch (OAuthMessageSignerException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (OAuthNotAuthorizedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (OAuthExpectationFailedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (OAuthCommunicationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
    
                //Important part where it actually sets the authorization so you can use it
                a = new AccessToken(httpOauthConsumer.getToken(),
                        httpOauthConsumer.getTokenSecret());
                twitter = new TwitterFactory().getInstance();
                twitter.setOAuthConsumer(consumerKey, consumerSecret);
                twitter.setOAuthAccessToken(a);
            }
        }
    
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    }
    

    要完成这项工作,您需要对 Manifest 进行一些调整。

    • 授予它使用互联网的权限:
        <uses-permission android:name="android.permission.INTERNET" />
    
    • 将启动模式设置为singleInstance
        <activity
        android:name="com.example.eredivisietwitter.MainActivity"
        android:label="@string/app_name"
        android:launchMode="singleInstance" >
    
    • 添加此意图过滤器
    <intent-filter>
        <action android:name="android.intent.action.VIEW" >
        </action>
    
        <category android:name="android.intent.category.DEFAULT" >
        </category>
    
        <category android:name="android.intent.category.BROWSABLE" >
        </category>
    
        <data
            android:host="HOST"
            android:scheme="SCHEME" >
        </data>
    </intent-filter>
    

    确保在您的 Activity 中使用相同的主机和方案:

    private final String CALLBACKURL = "SCHEME://HOST";
    

    现在您已成功授权您的应用,您可以使用Twitter 对象来请求时间线等。

    例子:

    private void getTweets(String user) {
    
        try {
            List<Status> statuses;
            statuses = twitter.getUserTimeline(user);
    
            System.out.println("Showing @" + user + "'s user timeline.");
            for (Status status : statuses) {
    
                System.out.println("@" + status.getUser().getScreenName()
                        + " - " + status.getText());
            }
    
        } catch (TwitterException te) {
            te.printStackTrace();
            System.out.println("Failed to get timeline: " + te.getMessage());
        }
    
    }
    

    瞧!

    【讨论】:

    • 但这里的问题是我刚刚收到推文,我还想要名称个人资料图片和时间....
    • 查看the javadoc 看看是否可以使用 twitter4j。在UserResources 中有一个showUser(long userId),它返回一个UserUser 然后有 getProfileImageURL() 等。
    • 我可以通过任何方法获取完整的 json,就像在 api 1.0 中一样??
    • 如何从推文中提取图片
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-28
    • 2012-11-19
    • 1970-01-01
    • 2014-01-07
    相关资源
    最近更新 更多