English 中文(简体)
Java(网络应用)到前游和前游
原标题:Login to twitter and tweet from Java (web-app)

我想写一个简单的java(或java网关)方案,使用户能够登录并张贴一名警子。 我甚至不需要用户接口。 我只能用硬手法来形容wit子、用户和密码。 我只想知道这一进程。 现在我一直在期待,我迄今没有成功。 下述最后本应奏效的法典并不奏效。

守则是一种简单的应用,而不是网络应用。 是否有任何法典将同本附录A合作? 我一直在试图使用前特4j。

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;

public class TwitterUtils {

public static void main(String[] args) {
    try {

        final String consumerKey = "**********";
        final String consumerSecret = "**********";
        Twitter twitter = new TwitterFactory().getInstance();
        twitter.setOAuthConsumer(consumerKey, consumerSecret);
        RequestToken requestToken = twitter.getOAuthRequestToken();

        String token = requestToken.getToken();
        String tokenSecret = requestToken.getTokenSecret();
        System.out.println("My token :: " + token);
        System.out.println("My token Secret :: " + tokenSecret);

        //AccessToken a = new AccessToken(token, tokenSecret);
        //twitter.setOAuthAccessToken(a);
        twitter.updateStatus("If you re reading this on Twitter, it worked!");

    } catch (TwitterException e) {
        e.printStackTrace();
    }
}//main
}//TwitterUtils

在“机会”线评论后,我发现错误。

Exception in thread "main" java.lang.IllegalStateException: Authentication credentials are missing. See http://twitter4j.org/configuration.html for the detail.     at twitter4j.TwitterBaseImpl.ensureAuthorizationEnabled(TwitterBaseImpl.java:205)
at twitter4j.TwitterImpl.updateStatus(TwitterImpl.java:453)
at playaround.TwitterUtils.main(TwitterUtils.java:55)

Java Result: 1

当我不按行文时,错误行文如下:

Exception in thread "main" java.lang.IllegalArgumentException: Invalid access token format. at twitter4j.auth.AccessToken.<init>(AccessToken.java:50)
at playaround.TwitterUtils.main(TwitterUtils.java:53)

Does anyone have a complete solution I may use? Thanks.

问题回答

Besides consumerKey and consumerSecret (your application s key and secret) you need accessToken from the user that is using your application. You get this accessToken from Twitter using OAuth protocol.

Through this link ( http://module.minic.ro/how-to-make-a-twitter-application-tutorial/ ) you can generate consumer key,consumer secret, access token, and access token secret keys

Use this code when you get keys for Twitter object:

ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("*********************")
.setOAuthConsumerSecret("******************************************")
.setOAuthAccessToken("**************************************************")
.setOAuthAccessTokenSecret("******************************************");
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();

rel=“nofollow”http://twitter4j.org/en/configuration.html





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签