尝试使用 twitter4j 更新状态时出现错误 401
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一周后,我发现问题出在当地时间的一台已关闭的服务器上。有人在 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.
随机数是一个被设计为只能使用一次的值。
我的猜测是,您用来生成随机数的代码正在保存随机数并尝试重用它。
我不熟悉 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.
对于您的“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.