使用 Twitter4j 时出现 401 身份验证凭据丢失或不正确
我正在为 Android 应用程序使用 twitter4j API。我已经给出了消费者密钥和消费者秘密。
导致此错误的原因可能是什么? 有没有适用于 twitter4j 的 android 示例代码?
Logcat 错误:
01-20 09:23:26.719: ERROR/!!! Error-->(565): 401:Authentication credentials were missing or incorrect.
01-20 09:23:26.719: WARN/System.err(565): 401:Authentication credentials were missing or incorrect.
01-20 09:23:26.719: WARN/System.err(565): TwitterException{exceptionCode=[fde06384-e892de6e], statusCode=401, retryAfter=0, rateLimitStatus=null, version=2.1.10}
01-20 09:23:26.719: WARN/System.err(565): at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:310)
01-20 09:23:26.738: WARN/System.err(565): at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:72)
01-20 09:23:26.738: WARN/System.err(565): at twitter4j.internal.http.HttpClientWrapper.get(HttpClientWrapper.java:94)
01-20 09:23:26.748: WARN/System.err(565): at twitter4j.Twitter.verifyCredentials(Twitter.java:1394)
01-20 09:23:26.748: WARN/System.err(565): at twitter4j.Twitter.getId(Twitter.java:212)
01-20 09:23:26.748: WARN/System.err(565): at twitter4j.AsyncTwitter.getId(AsyncTwitter.java:128)
01-20 09:23:26.748: WARN/System.err(565): at com.cabot.android.twittertest.MainTwitter.OAuthLogin(MainTwitter.java:121)
01-20 09:23:26.748: WARN/System.err(565): at com.cabot.android.twittertest.MainTwitter.onClick(MainTwitter.java:89)
01-20 09:23:26.766: WARN/System.err(565): at android.view.View.performClick(View.java:2485)
01-20 09:23:26.766: WARN/System.err(565): at android.view.View$PerformClick.run(View.java:9080)
01-20 09:23:26.766: WARN/System.err(565): at android.os.Handler.handleCallback(Handler.java:587)
01-20 09:23:26.766: WARN/System.err(565): at android.os.Handler.dispatchMessage(Handler.java:92)
01-20 09:23:26.766: WARN/System.err(565): at android.os.Looper.loop(Looper.java:123)
01-20 09:23:26.766: WARN/System.err(565): at android.app.ActivityThread.main(ActivityThread.java:3647)
01-20 09:23:26.781: WARN/System.err(565): at java.lang.reflect.Method.invokeNative(Native Method)
01-20 09:23:26.781: WARN/System.err(565): at java.lang.reflect.Method.invoke(Method.java:507)
01-20 09:23:26.781: WARN/System.err(565): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-20 09:23:26.781: WARN/System.err(565): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-20 09:23:26.781: WARN/System.err(565): at dalvik.system.NativeStart.main(Native Method)
01-20 09:23:29.487: DEBUG/dalvikvm(60): GC_CONCURRENT freed 937K, 46% free 4276K/7879K, external 1865K/3065K, paused 9ms+8ms
代码:
void OAuthLogin() {
try {
AsyncTwitter ATObj=new AsyncTwitter(username,password, this);
Twitter twitter=new Twitter();
twitter.setOAuthConsumer(TWITTER_CONSUMER_KEY, TWITTER_SECRET_KEY);
RequestToken twitterRequestToken= twitter.getOAuthRequestToken();
String token = twitterRequestToken.getToken();
String tokenSecret = twitterRequestToken.getTokenSecret();
AFobj.getBasicAuthorizationInstance (username,password);
int UserId=ATObj.getId();
ResponseList<UserList> AllUserList=twitter.getAllUserLists(UserId);
String URI=UserId+"";
Toast Display1=Toast.makeText(getBaseContext(), URI, Toast.LENGTH_LONG);
Display1.show();
String test="TEST01";
Toast Display=Toast.makeText(getBaseContext(), test, Toast.LENGTH_LONG);
Display.show();
} catch (Exception ex) {
Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
Log.e(" !!! Error-->", ex.getMessage());
ex.printStackTrace();
}
提前致谢。 :)
I am using twitter4j
API for an Android app. I have given the Consumer key and the Consumer secret.
What can be the reason for this error?
Is there any example code for android for twitter4j?
Logcat Error:
01-20 09:23:26.719: ERROR/!!! Error-->(565): 401:Authentication credentials were missing or incorrect.
01-20 09:23:26.719: WARN/System.err(565): 401:Authentication credentials were missing or incorrect.
01-20 09:23:26.719: WARN/System.err(565): TwitterException{exceptionCode=[fde06384-e892de6e], statusCode=401, retryAfter=0, rateLimitStatus=null, version=2.1.10}
01-20 09:23:26.719: WARN/System.err(565): at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:310)
01-20 09:23:26.738: WARN/System.err(565): at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:72)
01-20 09:23:26.738: WARN/System.err(565): at twitter4j.internal.http.HttpClientWrapper.get(HttpClientWrapper.java:94)
01-20 09:23:26.748: WARN/System.err(565): at twitter4j.Twitter.verifyCredentials(Twitter.java:1394)
01-20 09:23:26.748: WARN/System.err(565): at twitter4j.Twitter.getId(Twitter.java:212)
01-20 09:23:26.748: WARN/System.err(565): at twitter4j.AsyncTwitter.getId(AsyncTwitter.java:128)
01-20 09:23:26.748: WARN/System.err(565): at com.cabot.android.twittertest.MainTwitter.OAuthLogin(MainTwitter.java:121)
01-20 09:23:26.748: WARN/System.err(565): at com.cabot.android.twittertest.MainTwitter.onClick(MainTwitter.java:89)
01-20 09:23:26.766: WARN/System.err(565): at android.view.View.performClick(View.java:2485)
01-20 09:23:26.766: WARN/System.err(565): at android.view.View$PerformClick.run(View.java:9080)
01-20 09:23:26.766: WARN/System.err(565): at android.os.Handler.handleCallback(Handler.java:587)
01-20 09:23:26.766: WARN/System.err(565): at android.os.Handler.dispatchMessage(Handler.java:92)
01-20 09:23:26.766: WARN/System.err(565): at android.os.Looper.loop(Looper.java:123)
01-20 09:23:26.766: WARN/System.err(565): at android.app.ActivityThread.main(ActivityThread.java:3647)
01-20 09:23:26.781: WARN/System.err(565): at java.lang.reflect.Method.invokeNative(Native Method)
01-20 09:23:26.781: WARN/System.err(565): at java.lang.reflect.Method.invoke(Method.java:507)
01-20 09:23:26.781: WARN/System.err(565): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-20 09:23:26.781: WARN/System.err(565): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-20 09:23:26.781: WARN/System.err(565): at dalvik.system.NativeStart.main(Native Method)
01-20 09:23:29.487: DEBUG/dalvikvm(60): GC_CONCURRENT freed 937K, 46% free 4276K/7879K, external 1865K/3065K, paused 9ms+8ms
Code:
void OAuthLogin() {
try {
AsyncTwitter ATObj=new AsyncTwitter(username,password, this);
Twitter twitter=new Twitter();
twitter.setOAuthConsumer(TWITTER_CONSUMER_KEY, TWITTER_SECRET_KEY);
RequestToken twitterRequestToken= twitter.getOAuthRequestToken();
String token = twitterRequestToken.getToken();
String tokenSecret = twitterRequestToken.getTokenSecret();
AFobj.getBasicAuthorizationInstance (username,password);
int UserId=ATObj.getId();
ResponseList<UserList> AllUserList=twitter.getAllUserLists(UserId);
String URI=UserId+"";
Toast Display1=Toast.makeText(getBaseContext(), URI, Toast.LENGTH_LONG);
Display1.show();
String test="TEST01";
Toast Display=Toast.makeText(getBaseContext(), test, Toast.LENGTH_LONG);
Display.show();
} catch (Exception ex) {
Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
Log.e(" !!! Error-->", ex.getMessage());
ex.printStackTrace();
}
Thanks in advance. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
上面的代码不会编译,因为 AsyncTwitter 没有实现 getBasicAuthorizationInstance。
http://twitter4j.org/en/javadoc/twitter4j/AsyncTwitter.html
此外,不再支持基本身份验证。
您需要获取访问令牌才能调用需要身份验证的方法。
The above code won't compile since AsyncTwitter doesn't implement getBasicAuthorizationInstance.
http://twitter4j.org/en/javadoc/twitter4j/AsyncTwitter.html
Additionally, basic auth is not supported anymore.
You need to acquire access token to invoke methods that require authentication.