Magento 2 Rest API - Invalid signature Issue While Fetching All Products










0















I am working on Magento2 REST API with oAuth.
I am also using CURL to call REST API.



My API's are working If I call API's for Particular Customer Information or
Particular Product Information .



But When I call API to get all products then It will ask for searchCriteria.
And If I use search criteria then it is showing me error like "Invalid Signature". I have seen that many of us having this problem.



Is there any solution ?



Thanks in advanced.



My Code Snippet is as follows.




function sign($method, $url, $data, $consumerSecret, $tokenSecret)

$url = urlEncodeAsZend($url);

$data = urlEncodeAsZend(http_build_query($data, '', '&'));
$data = implode('&', [$method, $url, $data]);

$secret = implode('&', [$consumerSecret, $tokenSecret]);

return base64_encode(hash_hmac('sha1', $data, $secret, true));


function urlEncodeAsZend($value)

$encoded = rawurlencode($value);
$encoded = str_replace('%7E', '~', $encoded);
return $encoded;


$consumerKey = "myconsumerKey";
$consumerSecret = "myconsumerSecret";
$accessToken = "myaccessToken";
$accessTokenSecret = "myaccessTokenSecret";



$method = 'GET';

$searchCriteria = urlencode('searchCriteria[filterGroups][0][filters][0][field]=category_id&searchCriteria[filterGroups][0][filters][0][value]=1268&searchCriteria[filterGroups][0][filters][0][conditionType]=eq&searchCriteria[sortOrders][0][field]=created_at&searchCriteria[sortOrders][0][direction]=DESC&searchCriteria[pageSize]=10&searchCriteria[currentPage]=1');
//$searchCriteria['pageSize'] = 20;
$url = 'http://myhost.local/rest/V1/products?' . $searchCriteria;

//var_dump($url);exit;

//
$data = [
'oauth_consumer_key' => $consumerKey,
'oauth_nonce' => md5(uniqid(rand(), true)),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_timestamp' => time(),
'oauth_token' => $accessToken,
'oauth_version' => '1.0',
];

$data['oauth_signature'] = sign($method, $url, $data, $consumerSecret, $accessTokenSecret);
//var_dump($data['oauth_signature']);exit;
$curl = curl_init();

curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => [
'Authorization: OAuth ' . http_build_query($data, '', ',')
]
]);

$result = curl_exec($curl);
curl_close($curl);
var_dump($result);exit;









share|improve this question


























    0















    I am working on Magento2 REST API with oAuth.
    I am also using CURL to call REST API.



    My API's are working If I call API's for Particular Customer Information or
    Particular Product Information .



    But When I call API to get all products then It will ask for searchCriteria.
    And If I use search criteria then it is showing me error like "Invalid Signature". I have seen that many of us having this problem.



    Is there any solution ?



    Thanks in advanced.



    My Code Snippet is as follows.




    function sign($method, $url, $data, $consumerSecret, $tokenSecret)

    $url = urlEncodeAsZend($url);

    $data = urlEncodeAsZend(http_build_query($data, '', '&'));
    $data = implode('&', [$method, $url, $data]);

    $secret = implode('&', [$consumerSecret, $tokenSecret]);

    return base64_encode(hash_hmac('sha1', $data, $secret, true));


    function urlEncodeAsZend($value)

    $encoded = rawurlencode($value);
    $encoded = str_replace('%7E', '~', $encoded);
    return $encoded;


    $consumerKey = "myconsumerKey";
    $consumerSecret = "myconsumerSecret";
    $accessToken = "myaccessToken";
    $accessTokenSecret = "myaccessTokenSecret";



    $method = 'GET';

    $searchCriteria = urlencode('searchCriteria[filterGroups][0][filters][0][field]=category_id&searchCriteria[filterGroups][0][filters][0][value]=1268&searchCriteria[filterGroups][0][filters][0][conditionType]=eq&searchCriteria[sortOrders][0][field]=created_at&searchCriteria[sortOrders][0][direction]=DESC&searchCriteria[pageSize]=10&searchCriteria[currentPage]=1');
    //$searchCriteria['pageSize'] = 20;
    $url = 'http://myhost.local/rest/V1/products?' . $searchCriteria;

    //var_dump($url);exit;

    //
    $data = [
    'oauth_consumer_key' => $consumerKey,
    'oauth_nonce' => md5(uniqid(rand(), true)),
    'oauth_signature_method' => 'HMAC-SHA1',
    'oauth_timestamp' => time(),
    'oauth_token' => $accessToken,
    'oauth_version' => '1.0',
    ];

    $data['oauth_signature'] = sign($method, $url, $data, $consumerSecret, $accessTokenSecret);
    //var_dump($data['oauth_signature']);exit;
    $curl = curl_init();

    curl_setopt_array($curl, [
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => $url,
    CURLOPT_HTTPHEADER => [
    'Authorization: OAuth ' . http_build_query($data, '', ',')
    ]
    ]);

    $result = curl_exec($curl);
    curl_close($curl);
    var_dump($result);exit;









    share|improve this question
























      0












      0








      0








      I am working on Magento2 REST API with oAuth.
      I am also using CURL to call REST API.



      My API's are working If I call API's for Particular Customer Information or
      Particular Product Information .



      But When I call API to get all products then It will ask for searchCriteria.
      And If I use search criteria then it is showing me error like "Invalid Signature". I have seen that many of us having this problem.



      Is there any solution ?



      Thanks in advanced.



      My Code Snippet is as follows.




      function sign($method, $url, $data, $consumerSecret, $tokenSecret)

      $url = urlEncodeAsZend($url);

      $data = urlEncodeAsZend(http_build_query($data, '', '&'));
      $data = implode('&', [$method, $url, $data]);

      $secret = implode('&', [$consumerSecret, $tokenSecret]);

      return base64_encode(hash_hmac('sha1', $data, $secret, true));


      function urlEncodeAsZend($value)

      $encoded = rawurlencode($value);
      $encoded = str_replace('%7E', '~', $encoded);
      return $encoded;


      $consumerKey = "myconsumerKey";
      $consumerSecret = "myconsumerSecret";
      $accessToken = "myaccessToken";
      $accessTokenSecret = "myaccessTokenSecret";



      $method = 'GET';

      $searchCriteria = urlencode('searchCriteria[filterGroups][0][filters][0][field]=category_id&searchCriteria[filterGroups][0][filters][0][value]=1268&searchCriteria[filterGroups][0][filters][0][conditionType]=eq&searchCriteria[sortOrders][0][field]=created_at&searchCriteria[sortOrders][0][direction]=DESC&searchCriteria[pageSize]=10&searchCriteria[currentPage]=1');
      //$searchCriteria['pageSize'] = 20;
      $url = 'http://myhost.local/rest/V1/products?' . $searchCriteria;

      //var_dump($url);exit;

      //
      $data = [
      'oauth_consumer_key' => $consumerKey,
      'oauth_nonce' => md5(uniqid(rand(), true)),
      'oauth_signature_method' => 'HMAC-SHA1',
      'oauth_timestamp' => time(),
      'oauth_token' => $accessToken,
      'oauth_version' => '1.0',
      ];

      $data['oauth_signature'] = sign($method, $url, $data, $consumerSecret, $accessTokenSecret);
      //var_dump($data['oauth_signature']);exit;
      $curl = curl_init();

      curl_setopt_array($curl, [
      CURLOPT_RETURNTRANSFER => 1,
      CURLOPT_URL => $url,
      CURLOPT_HTTPHEADER => [
      'Authorization: OAuth ' . http_build_query($data, '', ',')
      ]
      ]);

      $result = curl_exec($curl);
      curl_close($curl);
      var_dump($result);exit;









      share|improve this question














      I am working on Magento2 REST API with oAuth.
      I am also using CURL to call REST API.



      My API's are working If I call API's for Particular Customer Information or
      Particular Product Information .



      But When I call API to get all products then It will ask for searchCriteria.
      And If I use search criteria then it is showing me error like "Invalid Signature". I have seen that many of us having this problem.



      Is there any solution ?



      Thanks in advanced.



      My Code Snippet is as follows.




      function sign($method, $url, $data, $consumerSecret, $tokenSecret)

      $url = urlEncodeAsZend($url);

      $data = urlEncodeAsZend(http_build_query($data, '', '&'));
      $data = implode('&', [$method, $url, $data]);

      $secret = implode('&', [$consumerSecret, $tokenSecret]);

      return base64_encode(hash_hmac('sha1', $data, $secret, true));


      function urlEncodeAsZend($value)

      $encoded = rawurlencode($value);
      $encoded = str_replace('%7E', '~', $encoded);
      return $encoded;


      $consumerKey = "myconsumerKey";
      $consumerSecret = "myconsumerSecret";
      $accessToken = "myaccessToken";
      $accessTokenSecret = "myaccessTokenSecret";



      $method = 'GET';

      $searchCriteria = urlencode('searchCriteria[filterGroups][0][filters][0][field]=category_id&searchCriteria[filterGroups][0][filters][0][value]=1268&searchCriteria[filterGroups][0][filters][0][conditionType]=eq&searchCriteria[sortOrders][0][field]=created_at&searchCriteria[sortOrders][0][direction]=DESC&searchCriteria[pageSize]=10&searchCriteria[currentPage]=1');
      //$searchCriteria['pageSize'] = 20;
      $url = 'http://myhost.local/rest/V1/products?' . $searchCriteria;

      //var_dump($url);exit;

      //
      $data = [
      'oauth_consumer_key' => $consumerKey,
      'oauth_nonce' => md5(uniqid(rand(), true)),
      'oauth_signature_method' => 'HMAC-SHA1',
      'oauth_timestamp' => time(),
      'oauth_token' => $accessToken,
      'oauth_version' => '1.0',
      ];

      $data['oauth_signature'] = sign($method, $url, $data, $consumerSecret, $accessTokenSecret);
      //var_dump($data['oauth_signature']);exit;
      $curl = curl_init();

      curl_setopt_array($curl, [
      CURLOPT_RETURNTRANSFER => 1,
      CURLOPT_URL => $url,
      CURLOPT_HTTPHEADER => [
      'Authorization: OAuth ' . http_build_query($data, '', ',')
      ]
      ]);

      $result = curl_exec($curl);
      curl_close($curl);
      var_dump($result);exit;






      php curl oauth magento2






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 5:10









      PrashantPrashant

      62




      62






















          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312805%2fmagento-2-rest-api-invalid-signature-issue-while-fetching-all-products%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312805%2fmagento-2-rest-api-invalid-signature-issue-while-fetching-all-products%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Darth Vader #20

          How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

          Ondo