1

사용자의 벽에 게시 할 수 있도록 Facebook oAuth를 PhoneGap 2.3.0 응용 프로그램과 통합하려고합니다. 라이브러리를 사용할 수 있습니다 : Phonegap oauth 이제는 InAppBrowser라고 불리는 childbrowser와 core Phonegap의 일부분에 대한 수정이 필요합니다 - 아래를보십시오.Facebook oAuth with 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

I는 다음과 같이 그 일을하고 있고 리디렉션 때 나는 code의 URL 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'가정하고 '그냥'loadstart'에 대한 이벤트 처리기를 잡기 위해 사용하는 가짜 URL일까요? Facebook에 성공 URL을 사용하고 싶지는 않습니다. 사용자에게 가장 알기 쉬운 경고가 아니기 때문입니다. –

+0

예이 URL에 도달하면 내 웹 사이트의 빈 페이지 URL입니다. URL에서 코드를 읽고 브라우저 창을 닫습니다. – ThinkFloyd

+0

이것은이 프로세스를 작동시키는 데 중요합니다. 콜백 URL에 리디렉션이있는 경우 리디렉션에 전달되지 않으므로 토큰이 손실되고 결국 끝나는 최종 URL에는 쿼리 문자열에 토큰이 없습니다. –