Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android and Twitter4j: Handling OAuth with a Webview widget?

I've got a working twitter4j implementation but the OAuth process for authorizing the app leaves the Android web browser running behind the application. I'd like to try implementing my own webview in a launched activity so I can finish() it or at least clean up after my app. Problem is, now I have to figure out how to return the authURL to my main activity.

What's the best way to return the authURL? I've subclassed a webview widget and am experimenting with a way to return the authURL in onPageFinished() but not quite there yet.

private class myWebViewClient extends WebViewClient
{
    @Override
    public void onPageFinished (WebView view, String url)
    {
    Log.d (TAG, "onPageFinished");
       super.onPageFinished (view, url);

       if (url.contains (TwitterLibActivity.CALLBACK_URL) == true)
       {
        /*
        mRetIntent = new Intent();
        mRetIntent.putExtra ("verifed", url);
        setResult (RESULT_OK, mRetIntent);
        */
        Log.d (TAG, "have auth url:" + url);
        finish();
       }
    }


    @Override
    public boolean shouldOverrideUrlLoading (WebView view, String url)
    {
        Log.d (TAG, "myWebViewClient url:" + url);
       //return super.shouldOverrideUrlLoading (view, url);
        return (false);
    }
}
like image 812
wufoo Avatar asked Feb 22 '12 16:02

wufoo


2 Answers

I recently found this article working only with twitter4j without singpost, this work with webview and can help you: http://davidcrowley.me/?p=410

Greetings

like image 171
ClarkXP Avatar answered Oct 17 '22 03:10

ClarkXP


TwitterOAuthView is a WebView subclass dedicated to Twitter OAuth on Android, using twitter4j.

Twitter OAuth View for Android using twitter4j
http://darutk-oboegaki.blogspot.jp/2012/07/twitter-oauth-view-for-android-using.html

Since it is implemented as a subclass of View, it can be integrated into the Android layout system seamlessly. This fact makes TwitterOAuthView an easily-reusable UI component.

Its usage is very simple. Just call start() method

// Start Twitter OAuth process. Getting a request token, opening Twitter's
// authorization page, and getting an access token are performed.
view.start(CONSUMER_KEY, CONSUMER_SECRET, CALLBACK_URL, true, listener);

and receive its result via TwitterOAuthView.Listener interface.

// Definition of TwitterOAuthView.Listener interface.
void onSuccess(TwitterOAuthView view, AccessToken accessToken);
void onFailure(TwitterOAuthView view, TwitterOAuthView.Result result);

An example of Activity implementation using TwitterOAuthView can be found at GitHub TwitterOAuthView.

like image 44
Takahiko Kawasaki Avatar answered Oct 17 '22 04:10

Takahiko Kawasaki