twitter4j:即使我已经设置了凭证错误?

发布于 2024-11-19 18:10:38 字数 2484 浏览 4 评论 0原文

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) 丢失或不正确。确保您已设置有效的消费者密钥/秘密、访问令牌/秘密以及同步的系统时钟。 错误 - 无法使用 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)

我已经在文件中设置了凭据,不是吗?

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: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)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凌乱心跳 2024-11-26 18:10:38

我发现这是一个工作代码:

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 应用程序
确保您的应用程序访问级别都为“读和写”

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文