I’ve been working on a Android app that posts workouts to Dailymile.com.
To authorise the app you need to log into dailymile via the web to retrieve an OAUth 2.0 token.
I do this by embedding a Android WebViewActivity
This all works fine if the user logs in to dailymile directly.
If the user logs in using the “Login with Facebook” button then there are problems.
- The user is asked for their facebook username
& password - Then to enter a name for the device (if not used before)
- At this point the browser redirects to a blank page
I’m using the following code to set up the WebView.
WebView webView = new WebView(this);
final Activity activity = this;
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setUserAgent(0);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if (progress == 100) {
activity.setTitle(R.string.app_name);
}
}
@Override
public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Log.d("Tracks2Miles", consoleMessage.message() + " -- From line "
+ consoleMessage.lineNumber() + " of "
+ consoleMessage.sourceId() );
return true;
}
@Override
public void onConsoleMessage(String message, int lineNumber,
String sourceID) {
Log.d("Tracks2Miles", message + " -- From line "
+ lineNumber + " of "
+ sourceID);
}
});
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i(TAG, url);
if (url.startsWith("dm://")) {
String summary = "<html><body>" + getResources().getString(R.string.auth_message) + "</body></html>";
view.loadData(summary, "text/html", "utf-8");
String token = url.substring(url.lastIndexOf("=")+1);
Log.i(TAG,token);
User user = Utilities.getAccountDetails(token, AuthenticatorActivity.this, handler);
if (user != null) {
user.setToken(token);
finishLogin(user);
}
return false;
}
view.loadUrl(url);
return true;
}
});
setContentView(webView);
webView.loadUrl(AUTH_URL);
The blank page seams to be the point where facebook would ask the user to actually grant permission to Dailymile.
Anybody got any ideas what I’m missing?
Having spent some more time looking into this last night it turns out it is because dailymile.com are returning a page with the wrong host/certificate pair.
After the user logs in Facebook is redirecting to a page hosted on http://www.dailymile.com and the certificate being presented is for api.dailymile.com.
I found the following blog post explains how to get the Android WebView to ignore the error.
http://damianflannery.wordpress.com/2010/09/28/android-webview-with-https-loadurl-shows-blankempty-page/