如何從 Twitter 檢測成功的身份驗證並在此之後運行一些代碼 (How to detect successful authentication from Twitter and run some code after that)


問題描述

如何從 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 MKBNikolay Elenkov)

參考文件

  1. How to detect successful authentication from Twitter and run some code after that (CC BY‑SA 3.0/4.0)

#twitter #Android #twitter4j






相關問題

用於輕鬆創建 Twitter 位置地理坐標的地圖工具? (Map-tool for easily creating Twitter location geo-coordinates?)

PHP Twitter OAuth - 我無法驗證我的帳戶 (PHP Twitter OAuth - I cannot authenticate my account)

如何設置文本光標在 TWTweetComposeViewController 中的位置? (How to set where the text cursor is in TWTweetComposeViewController?)

如何從 Twitter 檢測成功的身份驗證並在此之後運行一些代碼 (How to detect successful authentication from Twitter and run some code after that)

Cocos2d 2.0 升級後 Twitter 不再工作 (Twitter no longer working after Cocos2d 2.0 upgrade)

在 android (eclipse) 中集成 oAuth twitter 時清除共享首選項 (Clearing shared preferences in case of oAuth twitter integration in android (eclipse))

如何在 twitteR R 包的 getUser 中引用變量名? (How to refer a variable name in getUser in twitteR R package?)

使用 twitteR 包提取 Twitter 句柄而不是屏幕名稱? (Extract Twitter Handles instead of Screen Names using twitteR package?)

在白名單 PHP 之後獲取用戶電子郵件 (Get user email afer whitelist PHP)

多久運行一次 cron 來挖掘 Twitter 公共時間線? (How often to run the cron, to mine twitter public timeline?)

如何從 JQuery 發送到 SQL 數據庫? (How to send from JQuery to SQL database?)

使用 facebook、twitter 等進行身份驗證。合而為一! (Authentication with facebook, twitter, etc.. all in one!)







留言討論