2011-06-29 48 views
0

我在stream_publish致電時發生了Facebook錯誤。我實際上使用了Magento for Fconnect的擴展。 Fconnect & Flogin工作正常。但是,要求用戶下訂單時應將其張貼在用戶的牆上。對於我已經是這樣了FacebookFacebook中的錯誤簽名stream_publish致電

document.observe('click', function(e){ 

    if (e.element().match('a[rel^=facebook-connect]') || e.element().match('button[rel^=facebook-connect]')) { 

     e.stop(); 

     FB.login(function(response){ 



      if(response.status=='connected') setLocation('http://staging.mystore.com/facebook/customer_account/connect/'); 



     }, {perms:"email,publish_stream"}); 

    } 

}); 

客戶端文件generateSignature方法來實現是這樣

private function _generateSig($params_array) 
{ 
    Mage::log($params_array); 
    $str = ''; 
    ksort($params_array); 
    foreach ($params_array as $k=>$v) { 
     $str .= "$k=$v"; 
    } 

    $str .= $this->_secret; 
    Mage::log($str); 
    Mage::log('md5 sigs:: ' . md5($str)); 

    return md5($str); 
} 

&我的代碼,該代碼調用API是這樣

$message = 'just placed an order on mystore.com'; 

    $attachment = array(
    'name' => "mystore", 
    'href' => 'http://www.mystore.com/', 
    'description' => 'New order on mystore.com', 
    'media' => array(array('type' => 'image', 
    'src' => 'http://www.mystore.com/skin/frontend/default/mystore/images/logo.png', 
    'href' => 'http://www.mystore.com/'))); 

    $action_links = array(array('text' => '[email protected]', 'href' => 'http://www.mystore.com/')); 

    $attachment = json_encode($attachment); 
    $action_links = json_encode($action_links); 

    try{ 
     // if($facebook->api_client->stream_publish($message, $attachment, $action_links, null, $target_id)) 
     if($this->_getClient()->call('facebook.stream.publish', 
           array($message, $attachment, $action_links, 
             $this->_getClient()->users->getLoggedInUser(), 
             Mage::getSingleton('facebook/config')->getApiKey()) 
           ) ) 
     { 
      Mage::log("Added on FB Wall"); 
     } 

     } catch(Exception $e) 
     { 
      Mage::log("Exception in wall write"); 

      Mage::log($e); 
     } 

登錄後我在日誌中發現的簽名是

api_key=XXXXXXXXmethod=facebook.stream.publishsession_key=2.AQCm5fABfobInAS5.3600.1309352400.1-1000025660978090=just placed an order on mystore.comcall_id=1309345883.3068format=JSONv=1.01={"name":"mystore","href":"http:\/\/www.mystore.com\/","description":"New order on mystore.com","media":[{"type":"image","src":"http:\/\/www.mystore.com\/skin\/frontend\/default\/mystore\/images\/logo.png","href":"http:\/\/www.mystore.com\/"}]}2=[{"text":"[email protected]","href":"http:\/\/www.mystore.com\/"}]3=1000025660978094=5070afefb42b162aff748f55ecf44d110d9e2a90117ee1704e2adb41f1d190fa 

我從來沒有在Facebook上做過任何開發,所以我不知道該怎麼辦?請幫我解決。 &讓我知道如果你們需要任何其他信息來理解這一點。

噢一件事是調用API(調用方法)的客戶端文件代碼的這樣

private function _prepareParams($method, $params) 
{ 

    $defaultParams = array(
     'api_key' => $this->_apiKey, 
     'call_id' => microtime(true), 
     'format' => 'JSON', 
     'v'  => '1.0' 
    ); 

    if($this->_sessionKey){ 
     $defaultParams['session_key'] = $this->_sessionKey; 
    } 

    $params = array_merge($defaultParams, $params); 
    foreach ($params as $key => &$val) { 
     if (!is_array($val)) continue; 
     $val = Zend_Json::encode($val); 
    } 

    $params['method'] = $method; 

    if(isset($params['sig'])) { 
     unset($params['sig']); 
    } 
    $params['sig'] = $this->_generateSig($params); 

    return $params; 
} 


public function call($method, $args=array()) 
{ 
    Mage::log($args); 
    $params = $this->_prepareParams($method, $args); 

    $client = self::_getHttpClient() 
      ->setUri(self::FACEBOOK_REST_URI) 
      ->setMethod(Zend_Http_Client::POST) 
      ->resetParameters() 
      ->setParameterPost($params);  

    try { 
     $response = $client->request(); 
    } catch(Exception $e) { 
     throw new Mage_Core_Exception('Service unavaliable'); 
    } 

    if(!$response->isSuccessful()) { 
     throw new Mage_Core_Exception('Service unavaliable'); 
    } 

    $result = Zend_Json::decode($response->getBody()); 

    //json decode returns float on long uid number? is_json check? old php? 
    if(is_float($result)){ 
     $result = $response->getBody(); 
    } 

    if(is_array($result) && isset($result['error_code'])) { 
     throw new Mage_Core_Exception($result['error_msg'], $result['error_code']); 
    } 

    return $result; 
} 

對於調用API我用兩種方式$this->_getClient()->call('facebook.stream.publish', & $this->_getClient()->call('stream_publish', 他們都不是工作

回答

0

好友夥我找出這個錯誤

看看我的代碼

format=JSONv=1.01={"name":"mystore","href":"http:\/\/www.mystore.com\/","description":"New order on mystore.com","media":[{"type":"image","src":"http:\/\/www.mystore.com\/skin\/frontend\/default\/mystore\/images\/logo.png","href":"http:\/\/www.mystore.com\/"}]}2=[{"text":"[email protected]","href":"http:\/\/www.mystore.com\/"}]3=1000025660978094=5070afefb42b162aff748f55ecf44d110d9e2a90117ee1704e2adb41f1d190fa 

其中u可以看到格式= JSONv = 1.01 = {....} 2 = [{.....}]問題是我用數字數組爲參數。他們應該是相關陣列 像消息= {新秩序}附件= {....} 一旦我解決了關聯數組問題我的代碼開始正常工作 這裏是一個鏈接,會給你詳細的參數傳遞給stream.publish http://schoolout.net/en/developers/view/39

希望這也會幫助其他人。