package twitter4j.examples.tweets;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.conf.*;
import java.io.IOException;
public final class UpdateStatus {
public static void main(String[] args) throws IOException {
String testPost = "hello from otc";
String consumerKey = "key";
String consumerSecret = "secret";
String accessToken = "access";
String accessSecret = "access_secret";
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey(consumerKey)
.setOAuthConsumerSecret(consumerSecret)
.setOAuthAccessToken(accessToken)
.setOAuthAccessTokenSecret(accessSecret);
try {
TwitterFactory factory = new TwitterFactory();
Twitter twitter = factory.getInstance();
AccessToken accestoken = new AccessToken(accessToken, accessSecret);
twitter.setOAuthAccessToken(accestoken);
Status status = twitter.updateStatus(testPost);
System.out.println("it worked!");
if (status.getId() == 0) {
System.out
.println("Error occured while posting tweets to twitter");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
为什么我一直收到这个错误:
401:身份验证凭据(http://dev.twitter.com/pages/auth)丢失或不正确。确保您已设置有效的conumer密钥/密钥,访问令牌/密钥和系统时钟同步。 错误 - 无法通过OAuth进行身份验证。 请求 - /1/statuses/update.json 相关讨论可以在互联网上: http://www.google.co.jp/search?q=e06d87a8或 http://www.google.co.jp/search?q=5851cbdb TwitterException {exceptionCode = [e06d87a8-5851cbdb],statusCode = 401,retryAfter = -1,rateLimitStatus = null,featureSpecificRateLimitStatus = null,version = 2.2.3} 在twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:189) 在twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:65) 在twitter4j.internal.http.HttpClientWrapper.post(HttpClientWrapper.java:102) 在twitter4j.TwitterImpl.post(TwitterImpl.java:1871) 在twitter4j.TwitterImpl.updateStatus(TwitterImpl.java:459) 在twitter4j.examples.tweets.UpdateStatus.main(UpdateStatus.java:35)
我已经在文件中设置了凭据,不是吗?
答案 0 :(得分:2)
我认为这是一个有效的代码:
package twitter4j.examples.tweets;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import java.io.IOException;
public final class UpdateStatus {
public static void main(String[] args) throws IOException {
String tweet = "your first tweet via java";
String accessToken = "your access token";
String accessSecret = "your access token secret";
try {
TwitterFactory factory = new TwitterFactory();
Twitter twitter = factory.getInstance();
AccessToken accestoken = new AccessToken(accessToken, accessSecret);
twitter.setOAuthAccessToken(accestoken);
Status status = twitter.updateStatus(tweet);
System.out.println("it worked!");
if (status.getId() == 0) {
System.out
.println("Error occured while posting tweets to twitter");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
注意:为此,你必须在twitter上创建一个应用程序 你必须有一个包含以下
的twitter4j.properties文件debug set to true
oauth.consumerKey
oauth.consumerSecret
oauth.accessToken
oauth.accessTokenSecret
所有密钥和令牌都来自twitter应用程序 确保你的应用程序访问级别都说“读写”