twitter4j:即使我设置了凭据,也会出现凭据错误? [英] twitter4j: getting credential errors even though i had set them?

查看:350
本文介绍了twitter4j:即使我设置了凭据,也会出现凭据错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我为什么不断得到这个错误:

why do i keep getting this error:

401:身份验证凭据(http://dev.twitter.com/pages/auth)丢失或不正确。确保已设置有效的使用者密钥/机密,访问令牌/机密,并且系统时钟已同步。
错误-无法通过OAuth进行身份验证。
请求-/1/statuses/update.json
相关讨论可以在Internet上进行:
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,版本= 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)中的

401:Authentication credentials (http://dev.twitter.com/pages/auth) were missing or incorrect. Ensure that you have set valid conumer key/secret, access token/secret, and the system clock in in sync. error - Could not authenticate with OAuth. request - /1/statuses/update.json Relevant discussions can be on the Internet at: http://www.google.co.jp/search?q=e06d87a8 or http://www.google.co.jp/search?q=5851cbdb TwitterException{exceptionCode=[e06d87a8-5851cbdb], statusCode=401, retryAfter=-1, rateLimitStatus=null, featureSpecificRateLimitStatus=null, version=2.2.3} at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:189) at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:65) at twitter4j.internal.http.HttpClientWrapper.post(HttpClientWrapper.java:102) at twitter4j.TwitterImpl.post(TwitterImpl.java:1871) at twitter4j.TwitterImpl.updateStatus(TwitterImpl.java:459) at twitter4j.examples.tweets.UpdateStatus.main(UpdateStatus.java:35)

我已设定t他已经在文件中创建了凭据,不是吗?

i have set the credentials in the file already, have i not?

推荐答案

我想出了一个有效的代码:

i figured it out heres a working code:

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文件,其中包含以下内容

note: for this to work you MUST create an application on twitter you must have a twitter4j.properties file which contain the following

debug set to true
oauth.consumerKey 
oauth.consumerSecret 
oauth.accessToken 
oauth.accessTokenSecret

所有密钥和令牌均来自twitter应用程序
,请确保您的应用程序访问级别全部显示为读写

all the keys and tokens are from the twitter application make sure your application access level all say "read and write"

这篇关于twitter4j:即使我设置了凭据,也会出现凭据错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆