2

I use following code to call rest api in magento 2.1.5

    $consumerKey = 'aca1rh9env59axm0ob48xfu62t9s4a5e';
    $consumerSecret = 'dgn4y1ejfu9aixb5mrio4kbsor5tq208';
    $accessToken = 'fi5keosdaod7hgqsyhv9i8o2yxvk0dqx';
    $accessTokenSecret = 'ugeovkdcyvl3q2wc0kxjmgiprobtr63g';

    $method = 'GET';
    $url = 'https://dev.devserver.com/index.php/rest/V1/customers/1';

    $data = [
        //'searchCriteria' => '',
        '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'] = $this->sign($method, $url, $data, $consumerSecret, $accessTokenSecret);

    $curl = curl_init();

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

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

    public function sign($method, $url, $data, $consumerSecret, $tokenSecret)
    {
        $url = $this->urlEncodeAsZend($url);

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

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

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

    public function urlEncodeAsZend($value)
    {
        $encoded = rawurlencode($value);
        $encoded = str_replace('%7E', '~', $encoded);
        return $encoded;
    }

Everything works fine in this case. But if i change url to somethinf like this https://dev.devserver.com/index.php/rest/V1/products?searchCriteria[filter_groups][0][filters][0][field]=category_id&searchCriteria[filter_groups][0][filters][0][value]=10

i got error: invalid signature. Can anyone help with this?

1

Solution came suddenly. It looks like url and params were encoded wrongly. I use this library to ease rest api coding https://github.com/springimport/magento2-api-v1

    use springimport\magento2\apiv1\Configuration,
        springimport\magento2\apiv1\ApiFactory;

    ...

    $configuration = new Configuration;
    $configuration->setBaseUri('https://dev.devserver.com/index.php/rest/V1/customers/1');
    $configuration->setConsumerKey('aca1rh9env59axm0ob48xfu62t9s4a5e');
    $configuration->setConsumerSecret('dgn4y1ejfu9aixb5mrio4kbsor5tq208');
    $configuration->setToken('fi5keosdaod7hgqsyhv9i8o2yxvk0dqx');
    $configuration->setTokenSecret('ugeovkdcyvl3q2wc0kxjmgiprobtr63g');

    $apiFactory = new ApiFactory($configuration);
    $client = $apiFactory->getApiClient();

    $res = $client->request('GET', 'https://dev.devserver.com/index.php/rest/V1/products?searchCriteria[filter_groups][0][filters][0][field]=category_id&searchCriteria[filter_groups][0][filters][0][value]=10', ['verify' => false]);
    echo $res->getStatusCode();
    // 200
    echo $res->getHeaderLine('content-type');
    // 'application/json; charset=utf8'
    echo $res->getBody();
    die();

    ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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