我正在使用phonegap、adobe build 和facebook 使用这段代码进行身份验证。
我希望通过某种形式的回调来处理“成功”登录,并在用户的墙上发帖。
<script type="text/javascript">
var my_client_id = "133914256793487", // YOUR APP ID
my_secret = "862f10f883f8d91617b77b4b143abc8d", // YOUR APP SECRET
my_redirect_uri = "https://www.facebook.com/connect/login_success.html", // LEAVE THIS
my_type ="user_agent", my_display = "touch"; // LEAVE THIS
var facebook_token = "fbToken"; // OUR TOKEN KEEPER
var ref; //IN APP BROWSER REFERENCE
// FACEBOOK
var Facebook = {
init:function(){
// Begin Authorization
alert("we have begun");
var authorize_url = "https://www.facebook.com/dialog/oauth?";
authorize_url += "client_id=" + my_client_id;
authorize_url += "&redirect_uri=" + my_redirect_uri;
authorize_url += "&display=" + my_display;
authorize_url += "&scope=publish_stream";
//CALL IN APP BROWSER WITH THE LINK
ref = window.open(authorize_url, '_blank', 'location=no');
ref.addEventListener('loadstart', function(event){
Facebook.facebookLocChanged(event.url);
});
} ,
facebookLocChanged:function(loc){
if (loc.indexOf("https://www.facebook.com/connect/login_success.html") >= 0 ) {
//CLOSE INAPPBROWSER AND NAVIGATE TO INDEX
ref.close();
//THIS IS MEANT TO BE DONE ON SERVER SIDE TO PROTECT CLIENT SECRET
var codeUrl = 'https://graph.facebook.com/oauth/access_token?client_id='+my_client_id+'&client_secret='+my_secret+'&redirect_uri='+my_redirect_uri+'&code='+loc.split("=")[1];
console.log('CODE_URL::' + codeUrl);
$.ajax({
url: codeUrl,
data: {},
type: 'POST',
async: false,
cache: false,
success: function(data, status){
//WE STORE THE TOKEN HERE
localStorage.setItem(facebook_token, data.split('=')[1].split('&')[0]);
},
error: function(){
alert("Unknown error Occured");
}
});
}
}
}
</script>
<script type="text/javascript">
Facebook.init();
</script>
任何人都可以建议如何适当地扩展这个例子 - 我可以在哪里找到能够帮助自己的 API?目前,它成功地要求用户登录 - facebook 弹出 - 我成功接受,然后返回 Success - 并显示一条红色消息,说明用户应安全地保留此 URL。
干杯,
安迪