2014-10-03 68 views
0

我正在嘗試將事件發佈到我的IBM Social Business SmartCloud帳戶。我已經可以授予應用程序訪問權限並獲得訪問權限和刷新令牌。但是當發佈新的,甚至我得到一個401錯誤「否'Access-Control-Allow-Origin'標題出現在請求的資源上。」 IBM Social Business SmartCloud OAuth 2.0發佈新事件

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沒有工作。

$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, ), )));

現在的問題是其他人無法看到我的帖子,即使我們彼此跟隨。

獲得了用於將帶有視頻的og標記的網頁嵌入到我的ibm sb活動流中的json結構。因此視頻直接打開了我的縮略圖流,而不在新窗口中

 


    $json_data = '{"content":"https://mydomain.com/somevideo/", 
    "attachments":[{"objectType":"link", 
     "displayName":"My Display Name", 
     "url":"https://mydomain.com/somevideo/", 
     "summary":"My summary", 
     "image":{ 
      "url":"{thumbnail}/api/imageProxy?url=http%3a%2f%2fmydomain.com%2fsomevideo%2fthumbnail.jpg", 
       "alt":"My Display Name" 
      }, 
     "connections":{ 
      "video":{ 
       "connections":{"mime-type":"application/x-shockwave-flash"}, 
      "width":"853", 
      "height":"480", 
      "url":"https://mydomain.com/somevideo/" 
      } 
     } 
    } 
    ] 
    }'; 

 

鏈接出來,你郵寄到這個網址: https://apps.na.collabserv.com/connections/opensocial/rest/ublog/@me/@all?format=json

+0

我們不能夠CORS請求我們的後端服務。你可以看看是添加第二個頭到你的請求是授權:基本或授權:承載 2014-10-03 18:55:33

回答

0

只要你的代碼是在比一個不同的域託管apps.na.collabserv.com您將無法單獨使用JavaScript訪問REST API

在這種情況下,它是瀏覽器blocking you。由於後端未配置爲enable CORS requests,因此交叉原點標題將不起作用。

您可以解決此通過訪問REST API低谷的ajax proxy,部署在同一個域中的頁面

+0

謝謝你們似乎是我不能在PHP中使用cURL我不得不使用file_get_contents方法這很奇怪。 – 2014-10-08 13:36:55

+0

你可能想看看更詳細地描述PHP實現的https://github.com/OpenNTF/SocialSDK/wiki/Introducing-the-SBT-PHP-Core – 2014-10-09 11:27:02