facebook stream_publish调用中的签名不正确

时间:2011-06-29 11:17:54

标签: magento facebook

我在stream_publish调用时遇到了facebook错误。我实际上使用了Magento for Fconnect的扩展。 Fconnect& Flogin工作正常。但要求用户下订单时应将其张贴在用户的墙上。为此,我已经实现了这样的

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"});

    }

});

在Facebook客户端文件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' => 'Buy@mystore', '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":"Buy@mystore","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', 他们都没有工作

1 个答案:

答案 0 :(得分: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":"Buy@mystore","href":"http:\/\/www.mystore.com\/"}]3=1000025660978094=5070afefb42b162aff748f55ecf44d110d9e2a90117ee1704e2adb41f1d190fa

你可以看到 format = JSONv = 1.01 = {....} 2 = [{.....}] 问题是我使用数值数组作为参数。它们应该是关联的数组 例如 message = {new order} attachment = {....} 一旦我修复了关联数组问题,我的代码就开始正常工作了 这里有一个链接,它将详细说明要传递给stream.publish的参数 http://schoolout.net/en/developers/view/39

希望这也有助于其他人。