Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Created this to authenticate meetup for an android app.
import android.content.ContentValues;
import android.os.AsyncTask;
import android.util.Log;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
// leeloo oAuth lib
import net.smartam.leeloo.client.OAuthClient;
import net.smartam.leeloo.client.URLConnectionClient;
import net.smartam.leeloo.client.request.OAuthClientRequest;
import net.smartam.leeloo.client.response.OAuthAccessTokenResponse;
import net.smartam.leeloo.client.response.OAuthAuthzResponse;
import net.smartam.leeloo.common.exception.OAuthProblemException;
import net.smartam.leeloo.common.exception.OAuthSystemException;
import net.smartam.leeloo.common.message.types.GrantType;
import android.content.Intent;
import android.os.Bundle;
* Created with IntelliJ IDEA.
* Author: Adrian Maurer
* Date: 1/29/13
* Time: 7:46 PM
public class MeetupAuthActivity extends Activity {
private final String TAG = getClass().getName();
// Meetup OAuth Endpoints
public static final String AUTH_URL = "";
public static final String TOKEN_URL = "";
// Consumer
//public static final String REDIRECT_URI_SCHEME = "oauthresponse";
//public static final String REDIRECT_URI_HOST = "";
//public static final String REDIRECT_URI_HOST_APP = "yourapp";
//public static final String REDIRECT_URI = REDIRECT_URI_SCHEME + "://" + REDIRECT_URI_HOST + "/";
public static final String REDIRECT_URI = "";
public static final String CONSUMER_KEY = "yourconsumerkey";
public static final String CONSUMER_SECRET = "yourconsumersecret";
private WebView _webview;
private Intent _intent;
private Context _context;
public void onCreate(Bundle savedInstanceState) {
_intent = getIntent();
_context = getApplicationContext();
_webview = new WebView(this);
_webview.setWebViewClient(new MyWebViewClient());
OAuthClientRequest request = null;
try {
request = OAuthClientRequest.authorizationLocation(
} catch (OAuthSystemException e) {
Log.d(TAG, "OAuth request failed", e);
_webview.loadUrl(request.getLocationUri() + "&response_type=code&set_mobile=on");
public void finishActivity() {
//do something here before finishing if needed
private class MyWebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Uri uri = Uri.parse(url);
String code = uri.getQueryParameter("code");
String error = uri.getQueryParameter("error");
if (code != null) {
new MeetupRetrieveAccessTokenTask().execute(uri);
setResult(RESULT_OK, _intent);
} else if (error != null) {
setResult(RESULT_CANCELED, _intent);
return false;
private class MeetupRetrieveAccessTokenTask extends AsyncTask<Uri, Void, Void> {
protected Void doInBackground(Uri... params) {
Uri uri = params[0];
String code = uri.getQueryParameter("code");
OAuthClientRequest request = null;
try {
request = OAuthClientRequest.tokenLocation(TOKEN_URL)
OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
OAuthAccessTokenResponse response = oAuthClient.accessToken(request);
// do something with these like add them to _intent
Log.d(TAG, response.getAccessToken());
Log.d(TAG, response.getExpiresIn());
Log.d(TAG, response.getRefreshToken());
} catch (OAuthSystemException e) {
Log.e(TAG, "OAuth System Exception - Couldn't get access token: " + e.toString());
Toast.makeText(_context, "OAuth System Exception - Couldn't get access token: " + e.toString(), Toast.LENGTH_LONG).show();
} catch (OAuthProblemException e) {
Log.e(TAG, "OAuth Problem Exception - Couldn't get access token");
Toast.makeText(_context, "OAuth Problem Exception - Couldn't get access token", Toast.LENGTH_LONG).show();
return null;
public void onBackPressed()
setResult(RESULT_CANCELED, _intent);

thank you adrianmaurer! added the following lines in gradle for the code to work:
compile 'net.smartam.leeloo:oauth2-common:0.1'
compile 'net.smartam.leeloo:oauth2-client:0.1'

This is awesome! Totally worked for me.

Nice job, and thanks!

NixSam commented Jun 20, 2016

I had problem with OAuth Problem Exception - Couldn't get access token. It only happens once, and next time I request authentification, it worked ok. I changed a little bit your code, so to return data with token information in AsyncTas. So in part // do something with these like add them to _intent I did this:

   _intent.putExtra(Constants.MEETUP_ACCESS_TOKEN, response.getAccessToken());
   _intent.putExtra(Constants.MEETUP_EXPIRES_IN, response.getExpiresIn());
   _intent.putExtra(Constants.MEETUP_REFRESH_TOKEN, response.getRefreshToken());
   _intent.putExtra(Constants.MEETUP_BODY, response.getBody());

After that I've added onPostExecute like this:

protected void onPostExecute(Boolean aBoolean) {
   if (aBoolean) {
          setResult(RESULT_OK, mIntent);

I've also change creating of AsyncTask, so if I get the data in intent pass them on to activity who request it... That part looks like this:

if (code != null && !isFinishing()) {
                if (mIntent.hasExtra(Constants.MEETUP_BODY)) {
                    setResult(RESULT_OK, mIntent);
                } else {
                    new MeetupRetrieveAccessTokenTask().execute(uri);

So if someone else is experiencing the same problem, try my changes...
Anyway, thanks for great work @adrianmaurer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment