如何使用twitter4j LIB得到的网名的微博?如何使用、网名、twitter4j、LIB

2023-09-05 23:55:34 作者:坚强的泡沫

我已经看到了很多的教程使用该LIB但我不力得到它一个清晰的概念。

I have seen a lot of tutorials for using this lib but i dint get a clear idea of it.

首先我如何验证Twitter的应用程序??

Firstly how can i authenticate the twitter app??,

有没有什么方法可以让我很难c中的访问令牌$ C $,这样用户一点儿也不做任何事情,他可以通过输入画面名称??直接搜索特定用户的鸣叫

is there any way i can hardcode the access token, so that the user does'nt have to do anything he can directly search for the particular user's tweet by entering the screen name??

我怎样才能获得鸣叫提的屏幕名称??后

How can i get the tweets after mentioning a screen name??

我试着阅读文档与twitter4j的lib,但它不力帮助我....

I tried reading the docs with twitter4j lib but it dint help me....

我需要帮助的IM卡在此两天前,plz帮助...

I need help im stuck in this from two days , Plz help...

推荐答案

有多种方式进行身份验证:

There are multiple ways to authenticate:

标准的3条腿授权:我LL来解释这个简单的这个答案。

The standard 3-legged authorization : I'll be explaining this briefly in this answer.

品的授权:对于应用程序哪些不能访问或嵌入Web浏览器。

Pin-based authorization : For applications which cannot access or embed a web browser.

XAUTH :函数作为一个应用程序的授权,因此用户不需要登录,但使用应用程序授权。

xAuth : Functions as an Application Authorization, so the user does not need to log in but uses the application to authorize.

首先你需要在这里创建一个应用程序的。 然后,您将收到您的用户密钥和密码:

First of all you will need to create an application here. You will then receive your consumer key and secret:

您可以使用这个code在启动时请求授权。

You can then use this code to request authorization at start up.

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;
    }
}

为了使这项工作,你需要做一些调整,以你的清单。

To make this work you need to make a couple adjustments to your Manifest.

在给它的权限使用网络:
    <uses-permission android:name="android.permission.INTERNET" />

设置启动模式为 singleInstance

Set the launch mode to 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>

请务必在你的活动相同的主机和方案:

Make sure to have the same host and scheme in your Activity:

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

现在您已成功授权您的应用程序,你可以使用微博对象要求的时限等。

Now that you have successfully authorized your app you can use the Twitter object to request timelines etc.

例如:

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());
    }

}

瞧!