Sam*_*son 7 jquery facebook facebook-graph-api facebook-events
有没有办法使用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'">
Run Code Online (Sandbox Code Playgroud)
它使用参数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>
Run Code Online (Sandbox Code Playgroud)
我也尝试使用JQuery $ .post并手动输入URL以尝试创建此测试事件.返回:
XMLHttpRequest cannot load https://graph.facebook.com/me/events?. Origin http://localhost:8080 is not allowed by Access-Control-Allow-Origin.
Run Code Online (Sandbox Code Playgroud)
我也尝试将URL修改为/ User ID/events而不是/ me/events.Facebook不断回归:
({
"data": [
]
});
Run Code Online (Sandbox Code Playgroud)
如果我从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);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5314 次 |
| 最近记录: |