2

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... :'(

thelawnmowerman
  • 11,956
  • 2
  • 23
  • 36

1 Answers1

2

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.

Cormac Driver
  • 2,511
  • 1
  • 12
  • 10
  • Thank you VERY MUCH! You saved my day. I've tested your solution and works beautifully with Facebook and Twitter. However, it's a shame that Temboo website lacks of all this (despite the choreos docs are really great), as well as the needed configurations in Facebook and Twitter developer websites (and the Facebook one is not straight). – thelawnmowerman Dec 20 '13 at 00:34
  • Only one more question: I followed the advice here (http://support.temboo.com/entries/24131857-Using-OAuth-Choreos) of using this callback: "https://{Your Temboo Acount Name}.temboolive.com/callback/", but it seems to be broken (replacing my username, of course). It always gaves a HTTP ERROR 500 'Problem accessing /arcturus-web/callback/. Reason: Sorry, that's not a registered callback path!' – thelawnmowerman Dec 20 '13 at 00:38
  • All the proccess works, but I just wonder why that URL doesn't work. Once my Android app captures the URL redirection, I will close the webview and continue the process showing my own activity, but, shouldn't that web work? – thelawnmowerman Dec 20 '13 at 00:40
  • Thanks for letting us know about this. I don't have an answer for you right now but we'll investigate and figure out what's happening. Once quick question - did you also remove the brackets? Here's an example of what my URL looks like: https://cormac.temboolive.com/callback/ – Cormac Driver Dec 20 '13 at 14:03
  • Yes, of course. Your URL takes me to the same error page: `HTTP ERROR 500 Problem accessing /arcturus-web/callback/. Reason: Sorry, that's not a registered callback path!` – thelawnmowerman Dec 22 '13 at 11:50
  • Hi again. The URL we're discussing is the value that you need to put in the Site URL field of your Facebook app config, like this: https://www.evernote.com/shard/s143/sh/ced3c775-4507-4135-b290-7f2ba53c86f3/890f8a08463737cc49611d91403100bb/deep/0/Basic--Facebook-Developers.png. Once you've got that in there, you can use our Facebook InitializeOAuthChoreo to generate an AuthorizationURL that your users can visit to log into Facebook and grant your application access to their data. This process returns a callback ID, which you pass to the FinalizeOAuth Choreo. – Cormac Driver Dec 23 '13 at 18:49
  • Thank you very much for everything. @Cormac Driver, I would appreciate if you take a look to another question I have related to Temboo and Facebook: http://stackoverflow.com/questions/22836366/using-temboo-to-perform-a-facebook-like-action – thelawnmowerman Apr 03 '14 at 11:49
  • and another question also related to Temboo and Facebook: http://stackoverflow.com/questions/23367383/post-a-message-with-special-characters-on-facebook-through-temboo – thelawnmowerman Apr 29 '14 at 14:24
  • @CormacDriver can you please tell me how to use Disqus Temboo library in android – Yashpal Singla Apr 21 '15 at 14:16
  • @YashpalSingla I recommend checking out our Android getting started documentation and getting a basic example up and running before moving on to our Disqus support. If you have questions, don't hesitate to contact support@temboo.com. You can find the Android tutorials here: www.temboo.com/android – Cormac Driver May 14 '15 at 16:49