Skip to content

Instantly share code, notes, and snippets.

@4x4notfound
Last active January 12, 2017 04:46
Show Gist options
  • Select an option

  • Save 4x4notfound/4673944 to your computer and use it in GitHub Desktop.

Select an option

Save 4x4notfound/4673944 to your computer and use it in GitHub Desktop.

Revisions

  1. 4x4notfound revised this gist Jan 31, 2013. 1 changed file with 0 additions and 5 deletions.
    5 changes: 0 additions & 5 deletions MeetupAuthActivity.java
    Original file line number Diff line number Diff line change
    @@ -6,9 +6,6 @@
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.widget.Toast;
    import com.showup.app.R;
    import com.showup.app.SettingsActivity;
    import com.showup.provider.ShowUpProvider;

    // leeloo oAuth lib https://bitbucket.org/smartproject/oauth-2.0/wiki/Home
    import net.smartam.leeloo.client.OAuthClient;
    @@ -24,8 +21,6 @@
    import android.content.Intent;
    import android.os.Bundle;

    import java.util.Calendar;

    /**
    * Created with IntelliJ IDEA.
    * Author: Adrian Maurer
  2. 4x4notfound revised this gist Jan 31, 2013. 1 changed file with 6 additions and 10 deletions.
    16 changes: 6 additions & 10 deletions MeetupAuthActivity.java
    Original file line number Diff line number Diff line change
    @@ -35,15 +35,14 @@
    public class MeetupAuthActivity extends Activity {
    private final String TAG = getClass().getName();

    // Meetup OAuth
    // Endpoints
    // Meetup OAuth Endpoints
    public static final String AUTH_URL = "https://secure.meetup.com/oauth2/authorize";
    public static final String TOKEN_URL = "https://secure.meetup.com/oauth2/access";

    // Consumer
    public static final String REDIRECT_URI_SCHEME = "oauthresponse";
    public static final String REDIRECT_URI_HOST = "com.yourpackage.app";
    public static final String REDIRECT_URI_HOST_APP = "showup";
    //public static final String REDIRECT_URI_SCHEME = "oauthresponse";
    //public static final String REDIRECT_URI_HOST = "com.yourpackage.app";
    //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 = "your.redirect.com";
    public static final String CONSUMER_KEY = "yourconsumerkey";
    @@ -74,12 +73,11 @@ public void onCreate(Bundle savedInstanceState) {
    } catch (OAuthSystemException e) {
    Log.d(TAG, "OAuth request failed", e);
    }
    Log.d(TAG, request.getLocationUri()+ "&response_type=code&set_mobile=on");
    _webview.loadUrl(request.getLocationUri() + "&response_type=code&set_mobile=on");

    }

    public void finishActivity() {
    //do something here before finishing if needed
    finish();
    }

    @@ -92,8 +90,6 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
    String error = uri.getQueryParameter("error");

    if (code != null) {
    Log.i(TAG, code);

    new MeetupRetrieveAccessTokenTask().execute(uri);

    setResult(RESULT_OK, _intent);
    @@ -128,7 +124,7 @@ protected Void doInBackground(Uri... params) {

    OAuthAccessTokenResponse response = oAuthClient.accessToken(request);

    // do something with these
    // do something with these like add them to _intent
    Log.d(TAG, response.getAccessToken());
    Log.d(TAG, response.getExpiresIn());
    Log.d(TAG, response.getRefreshToken());
  3. 4x4notfound renamed this gist Jan 31, 2013. 1 changed file with 42 additions and 19 deletions.
    61 changes: 42 additions & 19 deletions MeetupAuth → MeetupAuthActivity.java
    Original file line number Diff line number Diff line change
    @@ -49,16 +49,22 @@ public class MeetupAuthActivity extends Activity {
    public static final String CONSUMER_KEY = "yourconsumerkey";
    public static final String CONSUMER_SECRET = "yourconsumersecret";

    private WebView webview;
    private WebView _webview;
    private Intent _intent;
    private Context _context;

    public void onCreate(Bundle savedInstanceState) {
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    webview = new WebView(this);
    webview.setWebViewClient(new MyWebViewClient());
    setContentView(webview);

    webview.getSettings().setJavaScriptEnabled(true);
    _intent = getIntent();
    _context = getApplicationContext();

    _webview = new WebView(this);
    _webview.setWebViewClient(new MyWebViewClient());
    setContentView(_webview);

    _webview.getSettings().setJavaScriptEnabled(true);
    OAuthClientRequest request = null;
    try {
    request = OAuthClientRequest.authorizationLocation(
    @@ -68,8 +74,9 @@ public void onCreate(Bundle savedInstanceState) {
    } catch (OAuthSystemException e) {
    Log.d(TAG, "OAuth request failed", e);
    }

    webview.loadUrl(request.getLocationUri()+ "&response_type=code&set_mobile=on");
    Log.d(TAG, request.getLocationUri()+ "&response_type=code&set_mobile=on");
    _webview.loadUrl(request.getLocationUri() + "&response_type=code&set_mobile=on");

    }

    public void finishActivity() {
    @@ -79,16 +86,23 @@ public void finishActivity() {
    private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
    Uri uri = Uri.parse(url);
    String code = uri.getQueryParameter("code");
    if (code != null) {
    Log.i(TAG, code);

    new MeetupRetrieveAccessTokenTask().execute(uri);

    finishActivity();
    }
    return false;
    Uri uri = Uri.parse(url);

    String code = uri.getQueryParameter("code");
    String error = uri.getQueryParameter("error");

    if (code != null) {
    Log.i(TAG, code);

    new MeetupRetrieveAccessTokenTask().execute(uri);

    setResult(RESULT_OK, _intent);
    finishActivity();
    } else if (error != null) {
    setResult(RESULT_CANCELED, _intent);
    finishActivity();
    }
    return false;
    }
    }

    @@ -99,7 +113,6 @@ protected Void doInBackground(Uri... params) {

    Uri uri = params[0];
    String code = uri.getQueryParameter("code");
    Log.i(TAG, code);

    OAuthClientRequest request = null;

    @@ -114,17 +127,27 @@ protected Void doInBackground(Uri... params) {
    OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());

    OAuthAccessTokenResponse response = oAuthClient.accessToken(request);
    //do something with tokens

    // do something with these
    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;
    }
    }

    @Override
    public void onBackPressed()
    {
    setResult(RESULT_CANCELED, _intent);
    finishActivity();
    }
    }
  4. 4x4notfound revised this gist Jan 30, 2013. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions MeetupAuth
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@
    // your package here

    import android.content.ContentValues;
    import android.net.Uri;
    import android.os.AsyncTask;
  5. 4x4notfound created this gist Jan 30, 2013.
    132 changes: 132 additions & 0 deletions MeetupAuth
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,132 @@
    // your package here

    import android.content.ContentValues;
    import android.net.Uri;
    import android.os.AsyncTask;
    import android.util.Log;
    import android.view.Window;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.widget.Toast;
    import com.showup.app.R;
    import com.showup.app.SettingsActivity;
    import com.showup.provider.ShowUpProvider;

    // leeloo oAuth lib https://bitbucket.org/smartproject/oauth-2.0/wiki/Home
    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.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;

    import java.util.Calendar;

    /**
    * 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 = "https://secure.meetup.com/oauth2/authorize";
    public static final String TOKEN_URL = "https://secure.meetup.com/oauth2/access";

    // Consumer
    public static final String REDIRECT_URI_SCHEME = "oauthresponse";
    public static final String REDIRECT_URI_HOST = "com.yourpackage.app";
    public static final String REDIRECT_URI_HOST_APP = "showup";
    //public static final String REDIRECT_URI = REDIRECT_URI_SCHEME + "://" + REDIRECT_URI_HOST + "/";
    public static final String REDIRECT_URI = "your.redirect.com";
    public static final String CONSUMER_KEY = "yourconsumerkey";
    public static final String CONSUMER_SECRET = "yourconsumersecret";

    private WebView webview;

    public void onCreate(Bundle savedInstanceState) {
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    webview = new WebView(this);
    webview.setWebViewClient(new MyWebViewClient());
    setContentView(webview);

    webview.getSettings().setJavaScriptEnabled(true);
    OAuthClientRequest request = null;
    try {
    request = OAuthClientRequest.authorizationLocation(
    AUTH_URL).setClientId(
    CONSUMER_KEY).setRedirectURI(
    REDIRECT_URI).buildQueryMessage();
    } catch (OAuthSystemException e) {
    Log.d(TAG, "OAuth request failed", e);
    }

    webview.loadUrl(request.getLocationUri()+ "&response_type=code&set_mobile=on");
    }

    public void finishActivity() {
    finish();
    }

    private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
    Uri uri = Uri.parse(url);
    String code = uri.getQueryParameter("code");
    if (code != null) {
    Log.i(TAG, code);

    new MeetupRetrieveAccessTokenTask().execute(uri);

    finishActivity();
    }
    return false;
    }
    }

    private class MeetupRetrieveAccessTokenTask extends AsyncTask<Uri, Void, Void> {

    @Override
    protected Void doInBackground(Uri... params) {

    Uri uri = params[0];
    String code = uri.getQueryParameter("code");
    Log.i(TAG, code);

    OAuthClientRequest request = null;

    try {
    request = OAuthClientRequest.tokenLocation(TOKEN_URL)
    .setGrantType(GrantType.AUTHORIZATION_CODE).setClientId(
    CONSUMER_KEY).setClientSecret(
    CONSUMER_SECRET).setRedirectURI(
    REDIRECT_URI).setCode(code)
    .buildBodyMessage();

    OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());

    OAuthAccessTokenResponse response = oAuthClient.accessToken(request);
    //do something with tokens
    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());
    } catch (OAuthProblemException e) {
    Log.e(TAG, "OAuth Problem Exception - Couldn't get access token");
    }
    return null;
    }
    }
    }