尝试使用 twitter4j 更新状态时出现错误 401

发布于 2024-10-01 19:09:37 字数 1954 浏览 5 评论 0原文

我正在尝试使用 twitter4j 更新 twitter 上的状态。在本地主机上一切正常,但是当我尝试在服务器上执行此操作时,出现授权错误:

401:身份验证凭据丢失或不正确。 {“错误”:“无效\/使用的随机数”,“请求”:“\/1\/statuses\/update.json”} TwitterException{exceptionCode=[15bb6564-00e4d61f],statusCode=401,retryAfter=0,rateLimitStatus=null,version=2.1.7-SNAPSHOT(内部版本:6498f401635aacf3388891cc5140b2086ff3f8aa)}

我在本地主机上使用“PIN”方法为用户生成了访问令牌。我认为它在哪里生成并不重要, accessToken.getToken()accessToken.getTokenSecret() 值不应该取决于我运行它们的机器,它们应该在任何地方工作,对吗?

这个错误信息是什么意思?我不明白“使用的随机数”部分。

更新

好吧,某处确实有些混乱。我现在正在尝试在服务器上生成访问令牌,甚至无法让它为我生成授权 URL:

Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer("twitterConsumerKey", "twitterConsumerSecret");

RequestToken requestToken = twitter.getOAuthRequestToken();
System.out.println(requestToken.getAuthorizationURL());

这失败了:

401:Authentication credentials were missing or incorrect.
Failed to validate oauth signature and token
TwitterException{exceptionCode=[68a00bf1-d29b9452], statusCode=401, retryAfter=0, rateLimitStatus=null, version=2.1.7-SNAPSHOT(build: 6498f401635aacf3388891cc5140b2086ff3f8aa)}
at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:308)
        at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:72)
        at twitter4j.internal.http.HttpClientWrapper.post(HttpClientWrapper.java:103)
        at twitter4j.http.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:124)
        at twitter4j.TwitterOAuthSupportBaseImpl.getOAuthRequestToken(TwitterOAuthSupportBaseImpl.java:87)
        at twitter4j.Twitter.getOAuthRequestToken(Twitter.java:54)
        at twitter4j.TwitterOAuthSupportBaseImpl.getOAuthRequestToken(TwitterOAuthSupportBaseImpl.java:80)
        at twitter4j.Twitter.getOAuthRequestToken(Twitter.java:54)

在 localhost 上一切正常。

I am trying to update status on twitter using twitter4j. Everything works fine on localhost, but when I try to do it on server I get authorization error:

401:Authentication credentials were missing or incorrect.
{"error":"Invalid \/ used nonce","request":"\/1\/statuses\/update.json"}
TwitterException{exceptionCode=[15bb6564-00e4d61f], statusCode=401, retryAfter=0, rateLimitStatus=null, version=2.1.7-SNAPSHOT(build: 6498f401635aacf3388891cc5140b2086ff3f8aa)}

I generated access token for a user using "PIN" method on localhost. I think it shouldn't matter where it was generated, accessToken.getToken() and accessToken.getTokenSecret() values shouldn't depend on which machine I run them, they should work anywhere, right?

What does this error message mean? I don't understand "used nonce" part.

UPDATE

Ok something is really messed up somewhere. I am trying to generate access token on a server now and can't even make it to generate authorization URL for me:

Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer("twitterConsumerKey", "twitterConsumerSecret");

RequestToken requestToken = twitter.getOAuthRequestToken();
System.out.println(requestToken.getAuthorizationURL());

This fails with:

401:Authentication credentials were missing or incorrect.
Failed to validate oauth signature and token
TwitterException{exceptionCode=[68a00bf1-d29b9452], statusCode=401, retryAfter=0, rateLimitStatus=null, version=2.1.7-SNAPSHOT(build: 6498f401635aacf3388891cc5140b2086ff3f8aa)}
at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:308)
        at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:72)
        at twitter4j.internal.http.HttpClientWrapper.post(HttpClientWrapper.java:103)
        at twitter4j.http.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:124)
        at twitter4j.TwitterOAuthSupportBaseImpl.getOAuthRequestToken(TwitterOAuthSupportBaseImpl.java:87)
        at twitter4j.Twitter.getOAuthRequestToken(Twitter.java:54)
        at twitter4j.TwitterOAuthSupportBaseImpl.getOAuthRequestToken(TwitterOAuthSupportBaseImpl.java:80)
        at twitter4j.Twitter.getOAuthRequestToken(Twitter.java:54)

On localhost everything works fine.

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

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

发布评论

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

评论(3

等数载,海棠开 2024-10-08 19:09:37

一周后,我发现问题出在当地时间的一台已关闭的服务器上。有人在 Twitter 上提供了清晰的错误消息,做得非常出色。

A week and few grey hairs later it turned out that the problem was in local time on a server which was off. Someone did a great job with clear error messages on Twitter.

若相惜即相离 2024-10-08 19:09:37

随机数是一个被设计为只能使用一次的值。

我的猜测是,您用来生成随机数的代码正在保存随机数并尝试重用它。

我不熟悉 twitter4j 库,但我会检查并确保您为每个请求生成一个新的随机数。

A nonce is a value that is designed to only be used once.

My guess is that the code you are using to generate a nonce is saving the nonce and trying to reuse it.

I am not familiar with the twitter4j library, but I would check and make sure you are generating a new nonce for each request.

香橙ぽ 2024-10-08 19:09:37

对于您的“401:身份验证凭据丢失或不正确”错误。确保您还将验证者 PIN 码包含在请求中作为签名基础的一部分。

For your "401:Authentication credentials were missing or incorrect" error. Make sure you also include the verifier pin with the request as part of the signature base.

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