2014-01-24 79 views
0

我正在嘗試使用Android Studio 0.4.2和Twitter4J庫開發自己的Android應用程序。我的想法是使用WebView在那裏加載Twitter認證頁面並從那裏獲取AccessToken。我可以獲得oauth_token和oauth_verifier,但是在那之後,所有Twitter方法都可以獲得關注者,發佈twits,無論什麼都不起作用。使用瀏覽器用戶身份驗證的Android4的Twitter4J錯誤:未發現身份驗證挑戰

這是我的代碼:

public class TwitterLoginFragment extends Fragment { 
    private static String TWITTER_CONSUMER_KEY = "***"; 
    private static String TWITTER_CONSUMER_SECRET = "***"; 
    private static final String TWITTER_CALLBACK_URL = "http://www.hita.pro"; 

    private static SharedPreferences sharedPreferences; 
    private Button btnTwitterLogin; 
    private Button btnTwitterLogOut; 
    private WebView wvTwitterLogin; 

    private IDs iDs; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     sharedPreferences = getActivity().getSharedPreferences("CONNECTION_INFO", Context.MODE_PRIVATE); 
     TwitterFactory.getSingleton().setOAuthConsumer(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET); 
     return inflater.inflate(R.layout.fragment_twitter_login, container, false); 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstance) { 
     super.onActivityCreated(savedInstance); 

     wvTwitterLogin = (WebView) getView().findViewById(R.id.wvTwitterLogin); 
     wvTwitterLogin.setWebViewClient(new WebViewClient() { 
      @Override 
      public boolean shouldOverrideUrlLoading(WebView view, String url) { 
       SharedPreferences.Editor editor = sharedPreferences.edit(); 
       editor.putBoolean("KEY_TWITTER_LOGIN", true); 
       editor.putString("OAUTH_TOKEN", url.substring(url.indexOf("oauth_token=") + 12, url.indexOf("&"))); 
       editor.putString("OAUTH_VERIFIER", url.substring(url.indexOf("oauth_verifier=") + 15)); 
       editor.commit(); 

       TwitterFactory.getSingleton().setOAuthAccessToken(new AccessToken(sharedPreferences.getString("OAUTH_TOKEN", ""), sharedPreferences.getString("OAUTH_VERIFIER", ""))); 

       new GetTwitterFollowers().execute(); 

       return true; 
      } 
     }); 

     btnTwitterLogin = (Button) getView().findViewById(R.id.btnTwitterLogin); 
     btnTwitterLogin.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       loginToTwitter(); 
      } 
     }); 

     btnTwitterLogOut = (Button) getView().findViewById(R.id.btnTwitterLogOut); 
     btnTwitterLogOut.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       twitterLogOut(); 
      } 
     }); 
    } 

    public void loginToTwitter() { 
     if (!isTwitterLoggedInAlready()) { 
      Thread thread = new Thread(new Runnable() { 
       @Override 
       public void run() { 
        try { 
         wvTwitterLogin.loadUrl(TwitterFactory.getSingleton().getOAuthRequestToken(TWITTER_CALLBACK_URL).getAuthenticationURL()); 

         getActivity().runOnUiThread(new Runnable() { 
          @Override 
          public void run() { 
           goToTwitterLogin(); 
          } 
         }); 
        } catch (Exception e) { 
         e.printStackTrace(); 
         Toast.makeText(getActivity(), getString(R.string.error_already_logged_twitter), Toast.LENGTH_LONG).show(); 
        } 
       } 
      }); 

      thread.start(); 
     } else 
      Toast.makeText(getActivity(), getString(R.string.error_already_logged_twitter), Toast.LENGTH_LONG).show(); 
    } 

    private void goToTwitterLogin() { 
     btnTwitterLogin.setVisibility(View.GONE); 
     btnTwitterLogOut.setVisibility(View.GONE); 
     wvTwitterLogin.setVisibility(View.VISIBLE); 
    } 

    private boolean isTwitterLoggedInAlready() { 
     return sharedPreferences.getBoolean("KEY_TWITTER_LOGIN", false); 
    } 

    private void twitterLogOut() { 
     SharedPreferences.Editor editor = sharedPreferences.edit(); 
     editor.putBoolean("KEY_TWITTER_LOGIN", false); 
     editor.commit(); 
    } 

    private class GetTwitterFollowers extends AsyncTask<Void, Void, Void> { 
     @Override 
     protected Void doInBackground(Void... v) { 
      try { 
       iDs = TwitterFactory.getSingleton().getFollowersIDs(-1); 
      } catch (TwitterException e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      iDs.getIDs(); 
     } 
    } 
} 

當我打電話給TwitterFactory.getSingleton()getFollowersIDs(-1)的用戶已經燒焦的後,我得到這個異常:

java.io.IOException: No authentication challenges found 

我絕望了,我花了一個星期試圖解決這個問題,但我在StackOverflow和其他網站中找到的所有線程都不起作用。系統時鐘正常,我嘗試過使用ConfigurationBuilder和其他解決方案,但沒有運氣。有人能幫助我嗎?

非常感謝!

回答

0

你是否嘗試過使用TwitterFactory中的非單例實例?

Uri uri  = Uri.parse(url); 
String token = uri.getQueryParameter("OAUTH_TOKEN"); 
String verifier = uri.getQueryParameter("OAUTH_VERIFIER"); 

Twitter twitter = TwitterFactory.getInstance(
     new AccessToken(token,verifier); 

twitter.getFollowers(-1);