2013-05-05 34 views
1

我想整合Facebook oAuth与我的Phonegap 2.3.0应用程序,以便我可以张贴到用户的墙上。我可以从这里使用库:Phonegap oauth进行了一些修改,以说明现在称为InAppBrowser的子浏览器和核心Phonegap的一部分 - 请参见下文。Facebook oAuth与Phonegap 2.3.0不返回令牌作为URL参数成功url

function FBConnect() 
{ 
    this.facebookkey = 'facebook'; 
    this.childBrowser = null; 
} 

FBConnect.prototype.connect = function(options) 
{ 
    this.client_id = options.consumerKey; 
    this.client_secret = options.consumerSecret 
    this.redirect_uri = options.callbackUrl; 
    oauth = OAuth(options); 
    var authorize_url = "https://m.facebook.com/dialog/oauth?"; 
     authorize_url += "client_id=" + this.client_id; 
     authorize_url += "&redirect_uri=" + this.redirect_uri; 
     authorize_url += "&display=touch"; 
     authorize_url += "&response_type=token"; 
     authorize_url += "&type=user_agent"; 
     authorize_url += "&scope=email,read_stream,publish_stream"; 

    var self = this; 
    self.childBrowser = window.open(authorize_url, '_blank', 'location=no'); 
    self.childBrowser.addEventListener('loadstart', function(event){ console.log('event fired: '+event);self.onLocationChange(event.url);}); 
} 

FBConnect.prototype.onLocationChange = function(loc) 
{ 

    console.log("onLocationChange : " + loc); 

    // If user hit "No, thanks" when asked to authorize access 
    if (loc.indexOf("login_success.html?error_reason=user_denied") >= 0) { 
     this.childBrowser.close(); 
     return; 
    } 

// here we get the code 
    if (loc.indexOf("access_token") >= 0) { 

     var access_token = loc.match(/access_token=(.*)$/)[1]; 
     console.log("facebook token" + access_token); 
     window.localStorage.setItem(window.plugins.fbConnect.facebookkey, access_token); 
     this.childBrowser.close(); 
     this.onConnect(); 
    } 
} 

我可以打开InAppBrowser将用户发送到授权页面。用户首先使用他们的Facebook帐户登录,然后看到应用程序页面,当他们单击确定时,他们就可以授予访问权限,然后是权限屏幕。然后用户将权限授予我的应用程序,然后发送到设置为:http://www.facebook.com/connect/login_success.html的callbackUrl。但是,在这个阶段,我希望将一个令牌附加到URL上作为查询参数。 URL中没有任何内容。

我错过了什么吗?

回答

1

我做这样的,它是为我工作时重定向,因为我越来越code网址PARAM:

openFBLogin : function() { 
    var my_client_id = Properties.FB_APP_ID, 
    my_redirect_uri = "http://www.myweb.com?fb_redirect=tabapp", 
    my_display  = "touch"; 

    var authorize_url = "https://graph.facebook.com/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_actions%2Cuser_birthday%2Cuser_interests%2Cuser_likes%2Cuser_location%2Cuser_relationships%2Cemail%2Cuser_checkins%2Cuser_hometown%2Cpublish_stream"; 
    Helper.iabRef = window.open(authorize_url, '_blank', 'presentationstyle=formsheet'); 
    Helper.iabRef.addEventListener('loadstop', Helper.iabFBLoginLoadStop); 
    Helper.iabRef.addEventListener('loadstart', Helper.iabFBLoginLoadStart); 
    console.log(Helper.iabRef); 
}, 
iabFBLoginLoadStart : function(event){ 
    Helper.onUrlChange(event.url); 
}, 
iabFBLoginLoadStop : function(event){ 
    Helper.onUrlChange(event.url); 
}, 
/** 
* Given the FB redirection URL it will check it for success/failure 
* If success it will do login else dump error message before closing the browser window 
* @param {} url 
*/ 
onUrlChange : function(url){ 
    var error = null; 
    if(url.indexOf("http://www.myweb.com") == 0){ 
     // User is redirected that means FB job is done 
     if(/code=/.test(url)){ 
      // If Code param is available that means authentication done 
      var fbCode = url.match(/code=(.*)$/)[1]; 
      console.log("logged in with fbCode = "+fbCode); 
      // TODO call login service with this code and 
      // on success save user credentials returned by service 
     } else if(/error_description=/.test(url)) { 
      // if error_description param is present that means login unsuccessful 
      error = (url.match(/error_description=(.*)$/)[1]).replace(/[^A-Za-z0-9 ]/g, " "); 
      console.log("Error message : "+error); 
     } 

     if(Helper.iabRef){ 
      Helper.iabRef.close(); 
     } 
    } 
    if(error){ 
     Ext.Msg.alert('Error','Unable to login using facebook : '+error); 
    } else{ 
     Ext.Msg.alert('Success','Thanks for logging in'); 
    } 
}, 
+0

我假设'http://www.myweb.com?fb_redirect=tabapp '只是一个伪URL,用于捕获'loadstart'的事件处理程序?我真的不想使用Facebook的成功网址,因为它对用户有红色警告 - 不是用户最友好的行为。 – 2013-05-06 11:42:55

+0

是的,这是我的网站上的空白页的URL,在到达该URL时我正在阅读来自URL的代码并关闭浏览器窗口 – ThinkFloyd 2013-05-07 05:58:03

+0

这对于使此过程起作用至关重要。如果您在回调网址上有任何重定向,则令牌将会丢失,因为它不会在重定向中传播,并且最终的最终网址不会在查询字符串中拥有令牌。 – 2013-05-13 13:53:03