使用JQuery和图形api以编程方式创建Facebook事件

有没有办法使用 JQuery函数以编程方式为用户Facebook页面创建事件?我创建了一个应用程序,通过以下方式询问用户create_event权限:
<input type="button" value="Add to Facebook" onclick="document.location='http://www.facebook.com/dialog/oauth?client_id=<AppID>&redirect_uri=<redirecturi>&scope=create_event,offline_access,manage_pages&response_type=token'">

它使用参数access_token和expires_in正确返回到重定向页面.该页面使用以下代码来解析数据(不太优雅,但我只是想让它作为测试工作)

<script>

$(document).ready(function(){
    var url = window.location.href;
    var fbParameters = url.substring(url.indexOf('#')+1,url.length);
    var accesstoken;    

    if(fbParameters.indexOf("access_token=")>=0){
        accesstoken = fbParameters.substring(fbParameters.indexOf("access_token=")+("access_token=").length,fbParameters.length);
        accesstoken=accesstoken.substring(0,accesstoken.indexOf('&'));
        console.log(accesstoken);
    }

    var params = {'access_token':accesstoken,'name':'test','location':'someplace','start_time':'1322719200'}

    $.getJSON('https://graph.facebook.com/me/events?callback=?',params,function(data){console.log(data)});

});

</script>

我也尝试使用JQuery $.post并手动输入URL以尝试创建此测试事件.返回:

XMLHttpRequest cannot load https://graph.facebook.com/me/events?. Origin http://localhost:8080 is not allowed by Access-Control-Allow-Origin.

我也尝试将URL修改为/ User ID / events而不是/ me / events. Facebook不断回归:

({
    "data": [

    ]
});

如果我从URL中删除“事件”,它会按预期访问用户信息.有谁知道我正在努力做到这一点真的可能吗?我觉得我错过了一些明显的东西.

解决方法

由于相同的源策略( http://en.wikipedia.org/wiki/Same_origin_policy),调用Graph API REST函数对于纯JQuery并不能很好地工作.

使用Facebook提供的JavaScript SDK是一个更好的主意,它创建一个隐藏的iframe来执行facebook域上的任务,并将事件传递回您的页面,非常好地处理所有身份验证和事件传递问题.

你可以在facebook开发者网站(http://developers.facebook.com/docs/reference/javascript/)和这里(http://developers.facebook.com/tools/console/)找到代码示例,文档和片段.

例如,创建一个事件如下:

var event = {  
    name: 'Name of your event',description: 'Description of your event',location: 'Location of event',start_time: Math.round(new Date().getTime()/1000.0),// Example Start Date
    end_time: Math.round(new Date().getTime()/1000.0)+86400 // Example End Date
};

FB.api('/me/events','post',event,function (result) {
    console.log(result); 
});

相关文章

jQuery表单验证提交:前台验证一(图文+视频)
jQuery表单验证提交:前台验证二(图文+视频)
jQuery如何实时监听获取input输入框的值
JQuery怎么判断元素是否存在
jQuery如何实现定时重定向
jquery如何获取复选框选中的值