Android Webview Facebook 登錄 (Android Webview Facebook Login)


問題描述

Android Webview Facebook 登錄 (Android Webview Facebook Login)

In my app, I want to make users be able to login with their Facebook account. After some searches, I am able to figure out this although I don't think this way is the best method. I used a webview in my UI, and a webviewclient to sense url switchings. As I understand, On Android, I must handle all redirections in my webviewclient (Facebook redirections, several redirections happen when user set the his/her email and password). But all I need is to parse my output xml and make a decision according to output result(redirect to my home activiy or failure etc). Here is my code, Please give your suggestions if more suitable method exists.

public class FbLoginActivity extends Activity {

    String fbLoginBaseUrl = "{my server url}/facebook/redirector.jsp?";
    private ProgressDialog progressBar; 
    WebView webView;
    int count = 0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.fb_login);

        fbLoginBaseUrl += "usdId=NoSession:";
        fbLoginBaseUrl += Subroutines.getInstance().getDeviceId() + "_L";

        webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        progressBar = ProgressDialog.show(FbLoginActivity.this, "", "Page is loading...");
        webView.setWebViewClient(new HelloWebViewClient());

        webView.loadUrl(fbLoginBaseUrl);

    }

    private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            Log.v(Subroutines.TAG, url);
            return true;
        }
        @Override
        public void onPageFinished(WebView view, String url) {
            Log.i(Subroutines.TAG, "Finished loading URL: " +url);

            // if login screen loading finishes, cancel the progressdialog..
            // twice redirecting happens to this sub url..
            String subFace = "m.facebook.com/login.php";
            if(url.indexOf(subFace) != -1 && ++count == 2){
                if (progressBar.isShowing()) {
                    progressBar.cancel();
                }
            }
            // Permission redirecting..
            String loginSub = "www.facebook.com/connect/uiserver.php?method=permissions.request";
            if(url.indexOf(loginSub) != -1){
                progressBar = ProgressDialog.show(FbLoginActivity.this, "", "Logging in...");
            }
            // finally if my server makes a response..
            String sub = "{my server url}/facebook/connect.jsp";
            if(url.indexOf(sub) != -1){
                Log.v(Subroutines.TAG, "my server makes a response..");

                // xml parsing stuff..
                // use url content to parse
            }
        }
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Log.e(Subroutines.TAG, "Error: " + description);
        }
    }
}

參考解法

方法 1:

I would get the Facebook SDK and use their functions

In a nutshell you do this:

public Facebook facebook = new Facebook("appID");

Then in on create or wherever:

facebook.authorize(this,String[] YourNeededPermissions, new DialogListener() {
        @Override
        public void onComplete(Bundle values) {}

        @Override
        public void onFacebookError(FacebookError error) {}

        @Override
        public void onError(DialogError e) {}

        @Override
        public void onCancel() {}
    });

You can see this here: http://developers.facebook.com/docs/guides/mobile/#android

方法 2:

I have actually a similar problem in integrating the facebook registration with my own (but I'm not the android developer). I asked the question here Facebook registration flow from Android but I didn't know about the webview. That seems a good solution, can I contact you by chat or other means? 

EDIT: Facebook just updated their login procedure: https://developers.facebook.com/docs/offline-access-deprecation/ as you can see, now the token you get from SSO (or any other client token) is linked only to your application (there is a validation token-app id-app secret). The endpoint is now 

https://graph.facebook.com/oauth/access_token?             
client_id=APP_ID&
client_secret=APP_SECRET&
grant_type=fb_exchange_token&
fb_exchange_token=EXISTING_ACCESS_TOKEN 

So using the facebook sdk for android the steps are: 1) get the user signed 2) send the token to the server 3) server will validate the token against its app id and app secret 4) add your own security features

(by barisatbasIamAlexAlrightRohi)

參考文件

  1. Android Webview Facebook Login (CC BY-SA 3.0/4.0)

#login #webview #webviewclient #Android #facebook






相關問題

只允許 oracle db 登錄到特定的應用程序? (Allowing oracle db login only to specific application?)

使用 FB.login 的註冊流程 (Registration flow using FB.login)

asp.net c# sistem login (asp.net c# login system)

Rails 設計登錄不工作 (Rails devise sign in not working)

我在 cakephp 2.4 中遇到了登錄頁面 (I'm getting stuck with login page in cakephp 2.4)

如何刪除特定用戶的會話登錄 (How to remove session login for specific user)

有沒有標準的asp.net認證授權登錄系統? (Is there a standard asp.net authentication authorization login system?)

所有這些網絡平台如何實現不需要用戶反复登錄的長時間登錄會話? (How do all these web platforms achieve a long-time login session that does not require the user to login over and over again?)

Util-Linux 登錄不使用影子密碼 (Util-Linux Login not working with shadow password)

Android Webview Facebook 登錄 (Android Webview Facebook Login)

重置 Bookstack 憑據 (Reset Bookstack Credentials)

輸入正確的詳細信息且未顯示錯誤後,登錄頁面未重定向到 index.php (Login in page not redirecting to index.php after entering correct details with no error displayed)







留言討論