2011-05-05 144 views
0

我正在致力於一個Facebook應用程序,我們只需要在一個textarea中提供一條消息。現在我們直接在用戶喜歡頁面後請求擴展權限(應用程序隱藏,直到用戶喜歡頁面)。我們希望它可以在提交表單時使用。Facebook Jquery ajax權限請求?

有沒有一種方法來加載彈出式樣div(不是新窗口... jquery.load?)中的權限對話框,並使用腳本頁作爲我的回調uri併發布我的數據而不重新加載選項卡?如果使用正常形式,我已經這樣做了,但是我們需要從FB中獲取名稱/圖片/電子郵件/ bday以放入數據庫。

任何幫助,即使是在正確的方向微調將非常感激!

/** ** UPDATED/

想和我想出了,它使用目前所有的軟件開發工具包更新。我試圖儘可能使其成爲template-y,以便其他人可以使用它。

$("#submit").live('click',function() { 
    if (someErrors === true) { 
     // handle errors 
    } else { 
     FB.login(function(response) { 
      if (response.session) { 
       if (response.perms) { 
        FB.api('/me', function(response) { 
         // vars: response.session.uid, response.name, response.email, response.birthday, etc 
         // serialize data here for post 
         $.post("process.php",formData,function(data) { 
          if (data.error) { 
           // send json encoded error messages back to retrieve here like below: 
           // PHP: echo json_encode(array("error"=>"fail")); 
          } else { 
           /** 
           send data back json encoded: 
           PHP: echo json_encode(array("yourData"=>"{$yourData}","fb_id"=>"{$fb_id}","name"=>"{$name}")); 
           Now we have data like: 
           var profile = 'http://www.facebook.com/profile.php?id='+data.fb_id; 
           var image = '<img src="http://graph.facebook.com/'+data.fb_id+'/picture" />'; 
           var yourData = data.yourData; 
           **/ 
          } 
         }, "json"); 
        }); 
       } else { 
        // more errors 
       } 
      } else { 
       // and more errors 
      } 
     }, {perms:'email,user_birthday'}); // request your specific permissions here 
    } 
    return false; 
}); 

回答

1

我必須做同樣的事情。我想出了這個:

 
$("#fb_login").click(function(){ 

      FB.login(function(response) { 
        if (response.session) 
        { 
         if (response.perms) 
         { 
           //alert("logged in..granted"); 
          var uid = FB.getSession().uid; 
         FB.api(
         { 
          method: 'fql.query', 
          query: 'SELECT name,email,pic FROM user WHERE uid=' + uid 
         }, 
         function(response) { 
         var user = response[0]; 
           $.get("addUser.php", { name: user.name, fbid: uid ,email: user.email }, function(data){ 
           // ajax call to addUser.php to insert into database. 


          }); 
          } 
          ); 
         } 
         else 
         { 
          // alert("Since You Have Not Granted Permissions For Accessing E-mails, You Can't register for any of the events."); 

         } 
        } 
        else 
        { 
         //alert("Not Logged In"); 
        } 
}, {perms:'publish_stream,email'}); // the extended permissions have to be specified here. 

您可以修改上述腳本以適應您的更改。 :)

+0

這幾乎就是我所需要的,但我已經在這裏結束了。這段代碼實際上使用了REST API。當我使用JS SDK完成時,我會發布我的代碼。謝謝! – 2011-05-05 06:52:34

+1

只需簡單的說明,不需要調用'FB.getSession()。uid'作爲會話對象中的id:'var uid = response.session.uid;' – ifaour 2011-05-05 18:07:27

1

權限對話框需要在彈出窗口或在Facebook的畫布頁面被加載,以防止點擊劫持,因此沒有,有沒有辦法做到這一點(雖然我很樂意被證明錯誤)。

,並澄清,通過彈出窗口,我的意思是在authentication documentation所示形式: FB auth dialog

可以火,當用戶提交表單這個彈出,就像這樣:

$('#submit_btn').click(function() // could also listen to the .submit() event 
{ 
    // show popup 
    FB.login(function(response) 
    { 
     // use FB methods to get data you want, 
     // attach it to valid input fields 

     // submit form 
     $('#form').submit(); 
    }); 
}); 
+0

是的,我認爲這是我正在尋找的方法。將回復並確認何時根據需要進行工作。再次感謝你! – 2011-05-05 05:21:35

+0

我想這不是,但是你知道當有人點擊頁面上方的按鈕時,如果不能重新加載頁面嗎? – 2011-05-05 07:22:00