問題描述
如何從 Twitter 檢測成功的身份驗證並在此之後運行一些代碼 (How to detect successful authentication from Twitter and run some code after that)
I am developing an android project in which I have Twitter integration(Using Twitter4j). Here is my code..,.
class BitmapDownloaderTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
Configuration configuration = builder.build();
TwitterFactory factory = new TwitterFactory(configuration);
twitter = factory.getInstance();
try {
requestToken = twitter.getOAuthRequestToken(TWITTER_CALLBACK_URL);
MainActivity.this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(requestToken.getAuthenticationURL())));
}catch(TwitterException e){e.printStackTrace();}
System.out.println("Fun");
return "Run";
}
@Override
protected void onPostExecute(String txt) {
if (isCancelled()) {
txt = null;
}
if(txt!=null){
System.out.println(txt);
Uri uri = getIntent().getData();
System.out.println(uri);
}
}
}
MY PROBLEM IS THAT TWITTER PAGE IS OPEN IN SEPARATE THREAD and "Fun" and "Run" is printed when I am still in Twitter page and when I return after Twitter authentication postexecute is already executed and hence I got null in uri..,.
Can somebody please help me, how can I run some code after Twitter authentication.
OR
Is there any method which run after successful authentication or after error like Facebook onComplete and cancel..,.
‑‑‑‑‑
參考解法
方法 1:
This will start the default browser, which is a separate application. You use a custom scheme in the callback URL and register your app as a handler, so you can get control back to your app, when authorization succeeds. If you want better control, you need to load the page in a WebView and intercept events to get notified. The whole OAuth thing integration is not trivial, but here's an article with an example project that describes is pretty well:
http://blog.doityourselfandroid.com/2011/08/08/improved‑twitter‑oauth‑android/
Also check other related articles on that blog.
(by MKB、Nikolay Elenkov)