Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android WebView with Facebook Login

I have an application that contains a webview. Inside the webview the user must log into Facebook using the default login button and authentication. In my webview, after the user logs in the oauth handshake url contains an "access denied...user denied your request" error. However, when I visit the same url in the core browser, everything works as expected and the user successfully gets logged in. I assume there are some methods I need to implement in the WebViewClient or WebChromeClient to give my webview full browser ability. However, I do not know what to do. Thoughts?

like image 950
deRonbrown Avatar asked Jan 07 '11 16:01

deRonbrown


1 Answers

Well you can use the facebook SDK for your implementation.

basically in a webview they have shown their content like this

private void setUpWebView(int margin) {
        LinearLayout webViewContainer = new LinearLayout(getContext());
        mWebView = new WebView(getContext());
        mWebView.setVerticalScrollBarEnabled(false);
        mWebView.setHorizontalScrollBarEnabled(false);
        mWebView.setWebViewClient(new FbDialog.FbWebViewClient());
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl(mUrl);
        mWebView.setLayoutParams(FILL);
        mWebView.setVisibility(View.INVISIBLE);
        mWebView.getSettings().setSavePassword(false);

        webViewContainer.setPadding(margin, margin, margin, margin);
        webViewContainer.addView(mWebView);
        mContent.addView(webViewContainer);
    }

    private class FbWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Util.logd("Facebook-WebView", "Redirect URL: " + url);
            if (url.startsWith(Facebook.REDIRECT_URI)) {
                Bundle values = Util.parseUrl(url);

                String error = values.getString("error");
                if (error == null) {
                    error = values.getString("error_type");
                }

                if (error == null) {
                    mListener.onComplete(values);
                } else if (error.equals("access_denied") ||
                           error.equals("OAuthAccessDeniedException")) {
                    mListener.onCancel();
                } else {
                    mListener.onFacebookError(new FacebookError(error));
                }

                FbDialog.this.dismiss();
                return true;
            } else if (url.startsWith(Facebook.CANCEL_URI)) {
                mListener.onCancel();
                FbDialog.this.dismiss();
                return true;
            } else if (url.contains(DISPLAY_STRING)) {
                return false;
            }
            // launch non-dialog URLs in a full browser
            getContext().startActivity(
                    new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            return true;
        }
like image 50
Aditya Pratap Singh Avatar answered Oct 15 '22 18:10

Aditya Pratap Singh