我正在为Android应用使用twitter4j
API。我给了消费者密钥和消费者秘密。
出现此错误的原因是什么? android for twitter4j有什么示例代码吗?
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();
}
提前致谢。 :)
答案 0 :(得分:1)
由于AsyncTwitter未实现getBasicAuthorizationInstance,因此上述代码无法编译。 http://twitter4j.org/en/javadoc/twitter4j/AsyncTwitter.html
此外,不再支持基本身份验证。 您需要获取访问令牌以调用需要身份验证的方法。