Android - AccountManager Twitter OAuth,未发现身份验证问题

时间:2015-03-30 15:56:57

标签: android twitter oauth twitter4j accountmanager

我正在尝试从AccountManager获取OAuth令牌以获取Twitter帐户,并使用它来使用 Twitter4j 关注Twitter句柄。但是我发现没有发现身份验证的挑战 TwitterException。

Bellow是获取令牌并跟随用户的代码:

 try{

     AccountManager accountManager = AccountManager.get(getActivity());
     Account twitterAccount = accountManager.getAccountsByType(AccountType.TWITTER.getType())[0];
 AccountManagerFuture<Bundle> tokenFuture =  accountManager.getAuthToken(twitterAccount, "com.twitter.android.oauth.token", null, true, new AccountManagerCallback<Bundle>() {
                    public void run(AccountManagerFuture<Bundle> result) {
                        Bundle bundle;
                        try {
                            bundle = result.getResult();
                            Intent intent = (Intent) bundle.get(AccountManager.KEY_INTENT);
                            if (intent != null) {
                                // User input required
                                getActivity().startActivity(intent);
                            } else {
                                String token = bundle.getString(AccountManager.KEY_AUTHTOKEN);
                                Log.i(TAG, "Token:" + token);
                              userToken = token;
                            }
                        } catch (Exception e) {
                            Log.e(TAG, "Error getting token", e);
                        }

                }

            }, null);

            AccountManagerFuture<Bundle> secretFuture =   accountManager.getAuthToken(twitterAccount, "com.twitter.android.oauth.token.secret", null, true, new AccountManagerCallback<Bundle>() {
                public void run(AccountManagerFuture<Bundle> result) {
                    Bundle bundle;
                    try {
                        bundle = result.getResult();
                        Intent intent = (Intent) bundle.get(AccountManager.KEY_INTENT);
                        if (intent != null) {
                            // User input required
                            getActivity().startActivity(intent);
                        } else {
                            String secret = bundle.getString(AccountManager.KEY_AUTHTOKEN);
                            Log.i(TAG, "Secret Token:" + secret);
                           userSecret = secret;

                        }
                    } catch (Exception e) {
                        Log.e(TAG, "Error getting token", e);
                    }

                }

            }, null);
            ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
            configurationBuilder
                    .setOAuthConsumerKey(<Consumer-key>);
            configurationBuilder
                    .setOAuthConsumerSecret(<Consumer-Secret>);

              configurationBuilder.setOAuthAccessToken(userToken);
            configurationBuilder.setOAuthAccessTokenSecret(userSecret);
            Twitter twitter = new TwitterFactory(configurationBuilder.build()).getInstance();
            User user = twitter.createFriendship(param.getUserName());
            Toast.makeText(followListActivity,"Followed user @"+user.getScreenName()+" :)",Toast.LENGTH_SHORT);


        }  catch (TwitterException e) {
            e.printStackTrace();
        }

获得此例外:

TwitterException{exceptionCode=[ec1fe56d-1e3b4ac5 3ea58453-a92e09a2], statusCode=-1, message=null, code=-1, retryAfter=-1, rateLimitStatus=null, version=4.0.2}
at twitter4j.HttpClientImpl.handleRequest(HttpClientImpl.java:178)
at twitter4j.HttpClientBase.request(HttpClientBase.java:53)
at twitter4j.HttpClientBase.get(HttpClientBase.java:71)
at twitter4j.TwitterBaseImpl.fillInIDAndScreenName(TwitterBaseImpl.java:128)
at twitter4j.TwitterImpl.verifyCredentials(TwitterImpl.java:545)
at asynctask.FollowTwitterUserAsyncTask.doInBackground(FollowTwitterUserAsyncTask.java:113)
 at asynctask.FollowTwitterUserAsyncTask.doInBackground(FollowTwitterUserAsyncTask.java:33)
 at android.os.AsyncTask$2.call(AsyncTask.java:287)
 at java.util.concurrent.FutureTask.run(FutureTask.java:234)
 at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
 at java.lang.Thread.run(Thread.java:841)
 Caused by: java.io.IOException: No authentication challenges found
at libcore.net.http.HttpURLConnectionImpl.getAuthorizationCredentials(HttpURLConnectionImpl.java:438)
at libcore.net.http.HttpURLConnectionImpl.processAuthHeader(HttpURLConnectionImpl.java:418)
at libcore.net.http.HttpURLConnectionImpl.processResponseHeaders(HttpURLConnectionImpl.java:367)
at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:301)
at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:497)
at libcore.net.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:134)
at twitter4j.HttpResponseImpl.<init>(HttpResponseImpl.java:35)
at twitter4j.HttpClientImpl.handleRequest(HttpClientImpl.java:142)

请指出我在这里做错了什么。

1 个答案:

答案 0 :(得分:0)

你根本没有做错任何事,这是图书馆的一个问题。在Android中,HTTP响应代码401会导致抛出IOException。库实现者checks for the message string of the IOException to catch it,但是您的消息字符串略有不同,因此异常处理不会执行。不幸的是,that GitHub project未启用问题跟踪器,但您可能会尝试联系the project owner,也许他会为您解决此问题。否则,您将不得不修改库代码并自行构建。