Question

I need to allow my app to post certain messages in user's Facebook and Twitter walls. The idea is as simple as presenting one button, and once the user clicks on it, he is requested to log in in his Facebook account (no matters the way) and give my app writing privilegies, and my app will be able to publish a certain message (or at least my app should be able to check if the user posted that certain message later). The same for Twitter with another button.

Facebook SDK and Twitter SDK for Android seem too scary at first, and I just want to log in and publish a message when user clicks on a button. I've been researching, and Temboo library is really promising. I've tested it and I am able to publish in Facebook and Twitter without any problem, providing the credentials of my own accounts. The problem is the login step to allow users publish in their own accounts.

Following Temboo procedures, I try to make use of the Choreos InitializeOAuth and FinalizeOAuth. If I am not mistaken, InitializeOAuth returns a callbackURL that must be presented to the user to allow my app to publish or whatever. But how? Do I need to load that URL in a webview and then detect when the user logs in inside the webview? I have no idea. Temboo website and powerful seem amazing, but documentation lacks of an example as useful as the entire process of authentication in Facebook or Twitter in Android... :'(

Was it helpful?

Solution

The short version is that you're right about how you'd implement the OAuth flow in an Android application. At a high level, your application will:

  1. Run the InitializeOAuth Choreo
  2. Open a WebView, pointed at the authorization URL returned by InitializeOAuth
  3. After the user clicks "allow" in the WebView, run the FinalizeOAuth Choreo to retrieve the access token(s)

The trick to #3 above is the ability to register custom URL handling schemes in Android, using "intent filters." In your AndroidManifest.xml file, you'll want to assign a custom intent filter to one of your activities, using code like this:

<activity android:name=".MyOAuthActivity">
<intent-filter>
<action android:name = "android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="temboo" android:host="twitter" />
</intent-filter>
</activity>

What this code means is that if your application receives a request for a url like "temboo://twitter" then that request will automatically be forwarded to the Activity that you specify -- in this case, MyOAuthActivity.

When you run the InitializeOAuth Choreo, you'll want to specify "temboo://twitter" (or whatever custom intent scheme you use) as the "forwarding URL" input. This will cause Temboo to forward the request back to your activity after the user clicks "Allow" in the OAuth webview.

In your Activity, you can then intercept URLs using your custom scheme, with code like this:

// Find the webview, and make sure Javascript is enabled.
WebView webview = (WebView)findViewById(R.id.oauthWebview);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient() { 


    // Here we override the onPageStarted method of the webview. If Twitter authorization
    // succeeds, we'll be redirected to a URL that looks like temboo://twitter
    public void onPageStarted(WebView view, String url, Bitmap favicon) {

        if(url.startsWith("temboo://")) {
            handled = true;
           // We got forwarded here from the 3rd party OAuth approval page; proceed
           // to next step
           Log.i("Temboo", "Got callback!");
           Intent i = new Intent(getBaseContext(), FinalizeOAuthActivity.class);
           i.putExtra("callbackID", callbackID);
           startActivity(i); 
        }
    } 
});

webview.loadUrl(authorizationURL);`

I work at Temboo by the way, so feel free to get in touch with any further questions you might have.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top