我正在尝试将事件发布到我的IBM Social Business SmartCloud帐户。我已经能够授予对应用程序的访问权限并获取访问权限并刷新令牌。但是当发布新内容时,即使我收到401错误" No' Access-Control-Allow-Origin'标头出现在请求的资源上。"
function postEvent(){
var postString = '{'+
'"actor": {'+
'"id": "@me"'+
'},'+
'"verb": "post",'+
'"title": "${share}",'+
'"content":"This event is my <b>first content</b>",'+
'"updated": "2012-01-01T12:00:00.000Z",'+
'"object": {'+
'"summary": "My Summary",'+
'"objectType": "note",'+
'"id": "someid",'+
'"displayName": "My displayName",'+
'"url": "mydomain.com"'+
'}}';
$.ajax({
url: 'https://apps.na.collabserv.com/connections/opensocial/basic/rest/activitystreams/@me/@all?format=json&access_token=<access_token>',
data: postString,
contentType: 'application/json',
method: 'POST',
dataType: 'json',
headers: {
// Set any custom headers here.
// If you set any non-simple headers, your server must include these
// headers in the 'Access-Control-Allow-Headers' response header.
'Content-Type: application/json',
'Origin': 'https://mydomain.com/',
'Access-Control-Allow-Headers' :'*',
'Access-Control-Allow-Origin': '*'
}
}).done(function(data) {
console.log(data);
});
}
function postEvent(){
var postString = '{'+
'"actor": {'+
'"id": "@me"'+
'},'+
'"verb": "post",'+
'"title": "${share}",'+
'"content":"This event is my <b>first content</b>",'+
'"updated": "2012-01-01T12:00:00.000Z",'+
'"object": {'+
'"summary": "My Summary",'+
'"objectType": "note",'+
'"id": "someid",'+
'"displayName": "My displayName",'+
'"url": "mydomain.com"'+
'}}';
$.ajax({
url: 'https://apps.na.collabserv.com/connections/opensocial/basic/rest/activitystreams/@me/@all?format=json&access_token=<access_token>',
data: postString,
contentType: 'application/json',
method: 'POST',
dataType: 'json',
headers: {
// Set any custom headers here.
// If you set any non-simple headers, your server must include these
// headers in the 'Access-Control-Allow-Headers' response header.
'Content-Type: application/json',
'Origin': 'https://mydomain.com/',
'Access-Control-Allow-Headers' :'*',
'Access-Control-Allow-Origin': '*'
}
}).done(function(data) {
console.log(data);
});
}
所以这是使用file_get_contents的代理方法我以前用它来运行php,cURL不起作用。
现在问题是,即使我们互相追随,其他人也看不到我的帖子。
获得了json结构,用于将带有视频og标记的网页嵌入到我的ibm sb活动流中。所以视频直接在我的流中打开,带有缩略图而不会在新窗口中链接
$post = file_get_contents('https://apps.na.collabserv.com/connections/opensocial/basic/rest/activitystreams/@me/@all?format=json',FALSE,stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => "Authorization: Bearer $access_token\r\n".
"Content-type: application/json\r\n".
"Content-length: " . strlen($json_data) . "\r\n",
'content' => $json_data,
),
)));
你发布到这个网址: https://apps.na.collabserv.com/connections/opensocial/rest/ublog/@me/@all?format=json
答案 0 :(得分:0)
只要您的代码托管在与apps.na.collabserv.com不同的域上,您将无法仅使用JavaScript访问REST API
在这种情况下,它是浏览器blocking you。交叉原始标头不起作用,因为后端未配置为enable CORS requests。
您可以通过ajax proxy访问REST API来解决此问题,该API部署在与您的网页相同的域中