Inappbrowser callback

自古美人都是妖i 提交于 2019-12-21 05:39:40

问题


I have tried to call back after login in facebook using inappbrowser. But it doesnt work to check the email,name etc.Here my code

Inappbrowser calling

function onDeviceReady(){   
     var my_client_id = FBkey,
     my_redirect_uri = "http://www.fastabuy.com/index.php",
     my_type = "user_agent",
     my_display = "touch";

     var authorize_url = FBgraphapiurl+"/oauth/authorize?";
     authorize_url += "client_id=" + my_client_id;
     authorize_url += "&redirect_uri=" + my_redirect_uri;
     authorize_url += "&display=" + my_display;
     authorize_url += "&scope=publish_stream,email,user_likes";
     isfir = "true";

     var ref = window.open(authorize_url, '_blank', 'location=yes');
     ref.addEventListener('loadstop', function facebookLocChanged() {
         facebookLoc(my_redirect_uri)
     });
}

Method for checking call back

  function facebookLoc(loc){
     if (loc.indexOf("http://www.fastabuy.com/index.php?") > -1){
       alert(loc.indexOf)
     }
  }

How to check when the url loc.indexOf>-1 to close the inappbrowser. Please help me to sort this out.


回答1:


1) You need to use loadstart event in InAppBrowser for checking redirected page.

2) After successful login use close method for closing InAppBrowser.

3) When you call facebookLoc function in callback, you should watch out for scope and i am using me variable to solve this problem.

onFacebookLogin: function() {

    var me = this;
    var appInBrowser = window.open(authorize_url, '_blank', 'location=yes');

    appInBrowser.addEventListener('loadstart', function(event) {
         me.facebookLoc(event.url,appInBrowser);
    });    
}

facebookLoc : function(loc,appInBrowser) {
   if (loc.indexOf("www.facebook.com/connect/login_success.html") > -1){
         alert('Login success');
         appInBrowser.close();
   }
}



回答2:


Here a sample https://github.com/raulduran/facebook-cordova

onDeviceReady: function() {
    app.onFacebookLogin();
},

onFacebookLogin: function() {

    var authorize_url  = "https://m.facebook.com/dialog/oauth?";
        authorize_url += "client_id=" + appId;
        authorize_url += "&redirect_uri=" + redirectUrl;
        authorize_url += "&display=touch";
        authorize_url += "&response_type=token";
        authorize_url += "&type=user_agent";

    if(permissions !== '') {
        authorize_url += "&scope=" + permissions;
    }

    var userDenied = false;
    var appInBrowser = window.open(authorize_url, '_blank', 'location=no');

    appInBrowser.addEventListener('loadstart', function(location) {

        if (location.url.indexOf("access_token") !== -1) {
            // Success
            var access_token = location.url.match(/access_token=(.*)$/)[1].split('&expires_in')[0];
            window.localStorage.setItem('facebook_accessToken', access_token);
            appInBrowser.close();
        }

        if (location.url.indexOf("error_reason=user_denied") !== -1) {
            // User denied
            userDenied = true;
            window.localStorage.setItem('facebook_accessToken', null);
            appInBrowser.close();
        }
    });
}


来源:https://stackoverflow.com/questions/18629961/inappbrowser-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!