2015-11-27 22 views
0

我想通過PHP發佈數組for api。我爲此使用了oAuth 1.0,但是如何使用oAuth來POST JSON體數組。我如何使用oAuth1.0 POST POST JSON body數組/ php

<? php 

//code start 

try 
{ 

    $postData = array(
     'ProductID' => $productID, 
     'InventoryType' => $InventoryType, 
     'ProductCostPrice' => $productCost 
    ); 

    $postData = json_encode($postData); 

    $oauth = new OAuth($this->consumer_key, $this->consumer_secret); 
    $oauth->enableDebug(); // simple debug flag for checking error messages 
    $oauth->disableSSLChecks(); // avoids an SSL issue when testing 

    $oauth->setToken($this->access_token, $this->token_secret); 

    ## POST JSON body array ## 
    $oauth->setBody('body',$postData); 
    ## 

    $url = $this->product_update_url.$productID; 

    $oauth->fetch($url, OAUTH_AUTH_TYPE_FORM); 

    $response_info = $oauth->getLastResponseInfo(); 
    header("Content-Type: {$response_info["content_type"]}"); 
    $dataList = (array) json_decode($oauth->getLastResponse()); 

    echo "<pre>"; 
    print_r($dataList); 
    echo "</pre>"; 
    exit; 



} catch(OAuthException $E) { 

    Mage::log('error', null, 'error.log'); 

} 

==================

網址:http://php.net/manual/en/class.oauth.php

請你能幫助,怎麼可以用我張貼JSON體陣列的OAuth。

回答

0

的方式請求參數進行歸一化的OAuth1 spec意味着你應該只簽署體,如果它在一個表單URL編碼格式(即param1=value1&param2=value2arr[]=val1&arr[]=val2

這是由於方式您好!OAuth1計算請求籤名,其中包括按字母順序排列參數。

在你上面的例子中,如果可能的話,你應該嘗試使用url編碼數據數組。

$postData = array(
    'ProductID' => $productID, 
    'InventoryType' => $InventoryType, 
    'ProductCostPrice' => $productCost 
); 
// "ProductID=1&InventoryType=widget&ProductCostPrice=3.5" 
$body = http_build_query($postData); 
... 
$oauth->setBody('body', $body); 

如果無論出於何種原因這是不可能的,那麼在解碼請求時,需要確保請求主體不用於簽名計算。

如果您使用的HTTPS API(您應該),您應該沒有在簽名中驗證的請求正文。如果你允許HTTP請求到你的API,你應該有一些機制來確認請求主體沒有被改變。

我見過的方法是散列JSON主體字符串,然後將該散列作爲查詢參數包含在請求中(因此它包含在簽名計算中)。然後,作爲API解碼過程的一部分,您需要確認JSON正文哈希符合簽名查詢值。

這個問題有點舊,但我通過谷歌搜索發現它,所以我認爲最好給未來的搜索者留下一個答案。