7

i am using magento rest api for get data and it work very well but when i wanna send data return this error

   "messages": {
        "error": [
            {
                "code": 401,
                "message": "oauth_problem=signature_invalid"
            }
        ]
    }

i use post man for this ... is there any one that can tell me why i get this error ?

1
  • This is some Magento core/The client using to generate OAuth Signature issue. The client generated signature and Magento generated signatures are mismatching that's the reason behind throwing this error. For temporary testing, to works fine, you can just comment the Magento code that checks the signature and through errors, if it's mismatch Here are the lines that check sinatures. On file app/code/core/Mage/Oauth/Model/Server.php Line number 547 to 549 if ($calculatedSign != $this->_protocolParams['oauth_signature']) { $this->_throwException('', self::ERR_SIGNATURE_INVALID); } Here you can see
    – Arshad M
    May 27, 2017 at 10:25

5 Answers 5

7

This is some Magento core/The client using to generate OAuth Signature issue.

The client generated signature and Magento generated signatures are mismatching that's the reason behind throwing this error.

For temporary testing, to works fine, you can just comment the Magento code that checks the signature and through errors, if it's mismatch

Here are the lines that check sinatures.

On file app/code/core/Mage/Oauth/Model/Server.php

Line number 547 to 549

if ($calculatedSign != $this->_protocolParams['oauth_signature']) {
     $this->_throwException('', self::ERR_SIGNATURE_INVALID);
}

Here you can see where $calculatedSign is the Magento calculated signature and $this->_protocolParams['oauth_signature'] is what we sending.

For testing purpose, you can comment this lines and work.

1
  • When we have live app and site then how we create signature? Sep 5, 2017 at 12:00
2

I have encountered the same problem recently.

I managed to make it work by changing the Signature Method from HMAC-SHA1 to PLAINTEXT on the authentication header.

1

Did you Encode the names and values of parameters?

I was having this issue too and this helped me.

You have to make sure that your signature base string parameters are in alphabetic order by parameter name

This is what i'm using to encode parameters in my signature base.

var result = new StringBuilder();
string webUnreservedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";
foreach (char symbol in url)
{
    if (webUnreservedChars.IndexOf(symbol) != -1)
        result.Append(symbol);
    else
        result.Append('%' + ((int)symbol).ToString("X2"));
}
return result.ToString();
1
  • thanks but i use post man and the post man auto create signatuer Nov 24, 2016 at 11:15
0

I was using AFOAuth1Client in an iOS app to talk to the Magento Rest API. Switching from

[client setSignatureMethod:AFHMACSHA1SignatureMethod];

to

[client setSignatureMethod:AFPlainTextSignatureMethod];

as per Cazo's answer, resolved this issue.

0

Had the same issue with the encoding, I had only the brackets and the space to encode so I done it with str_replace. Here is my example

<?php 

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$apiResources = "orders?";
//$apiResources = "orders?limit=2";

$apiResources .= 'filter[1][attribute]=created_at&filter[1][gt]=2023-01-01 00:00:00';
$apiResources = str_replace('[','%5B',$apiResources);  //these lines needs for encoding , for some reason the url_encode wasnt working
$apiResources = str_replace(']','%5D',$apiResources);
$apiResources = str_replace(' ','%20',$apiResources);
$apiResources .= '&limit=100&page=1';


$host = 'https://www.example.com/';
$consumerKey    = '41c423f6ba55ea2430b1fe3e4a13dfbf4f';
$consumerSecret = '2f9821b14d752c52424c5691e4a068a08f1a';
$token = '50fb626d942babe37aa9313424d775e6313';
$secret = 'c13a20eef307bf3c473b4242cbcc21116440';
$apiUrl = $host . "api/rest/";



try {

$oauthClient = new OAuth($consumerKey, $consumerSecret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION);
$oauthClient->setToken($token, $secret);
$oauthClient->enableDebug();

$resourceUrl = $apiUrl. $apiResources;



$oauthClient->fetch($resourceUrl, array(), 'GET', array('Content-Type' => 'application/json', 'Accept' => '*/*'));
$orderList = $oauthClient->getLastResponse();


print_r($orderList);

} catch (OAuthException $e) {
    echo "<pre>";
    print_r($e->getMessage());
    echo "<br/>";
    print_r($e->lastResponse);
    echo "</pre>";
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.