Created
January 19, 2016 01:35
-
-
Save cbetz/62978e90c3beb30a56d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.annotation.SuppressLint; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.graphics.Bitmap; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.webkit.CookieManager; | |
import android.webkit.CookieSyncManager; | |
import android.webkit.WebView; | |
import android.webkit.WebViewClient; | |
public class UntappdOAuthActivity extends Activity { | |
private static final String TAG = "UntappdOAuthActivity"; | |
private WebView webview; | |
@SuppressLint("SetJavaScriptEnabled") | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_untappd_oauth); | |
Bundle extras = getIntent().getExtras(); | |
String clientId = extras.getString("clientId"); | |
String redirectUrl = extras.getString("redirectUrl"); | |
String url = | |
"https://untappd.com/oauth/authenticate" + | |
"?client_id=" + clientId + | |
"&response_type=token" + | |
"&redirect_url=" + redirectUrl; | |
// "After the user has logged in we will redirect back to the following address: | |
// http://REDIRECT_URL#access_token=TOKENHERE" | |
webview = (WebView)findViewById(R.id.webView); | |
webview.getSettings().setJavaScriptEnabled(true); | |
// clear cookies | |
CookieSyncManager.createInstance(this); | |
CookieManager cookieManager = CookieManager.getInstance(); | |
cookieManager.removeAllCookie(); | |
webview.setWebViewClient(new WebViewClient() { | |
public void onPageStarted(WebView view, String url, Bitmap favicon) { | |
String fragment = "#access_token="; | |
int start = url.indexOf(fragment); | |
if (start > -1) { | |
String accessToken = url.substring(start + fragment.length(), url.length()); | |
Log.v(TAG, "Token: " + accessToken); | |
BeerSpecApplication app = ((BeerSpecApplication) getApplication()); | |
app.saveAccessToken(accessToken); | |
app.setAuthenticated(true); | |
Intent intent = new Intent(UntappdOAuthActivity.this, MainActivity.class); | |
startActivity(intent); | |
finish(); | |
} | |
} | |
}); | |
webview.loadUrl(url); | |
} | |
@Override | |
protected void onResume() { | |
// TODO Auto-generated method stub | |
super.onResume(); | |
} | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
webview.removeAllViews(); | |
webview.destroy(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment