A
A
Alexander2020-12-08 11:44:59
PHP
Alexander, 2020-12-08 11:44:59

How to pass additional parameters when using OAuth?

Good afternoon!

You need to get a list of all API orders from etsy.com.
5fcf3a7fbb951866608677.png
The problem is that when passing any of the parameters, I get an error. For example, I pass limit=10 :

oauth_problem=signature_invalid &debug_sbs=GET&https%3A%2F%2Fopenapi.etsy.com%2Fv2%2Fshops%2FWoodPecStudio%2Freceipts&limit%3D10%26oauth_consumer_key..


If you do not pass any parameters, then everything works. Here is the code itself:
function buildBaseString($baseURI, $method, $params)
{
    $r = array(); 
    ksort($params);
    foreach($params as $key=> $value){
        $r[] = "$key=" . rawurlencode($value); 
    }
    return $method."&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r)); 
}

function buildAuthorizationHeader($oauth)
{
    $r = 'Authorization: OAuth '; 
    $values = array(); 
    foreach($oauth as $key=> $value)
        $values[] = "$key=\"" . rawurlencode($value) . "\""; 
    $r .= implode(', ', $values); 
    return $r; 
}

$url = "https://openapi.etsy.com/v2/shops/".ETSY_PROJECT."/receipts";

$oauth_access_token = ETSY_OAUTH_TOKEN;
$oauth_access_token_secret = ETSY_OAUTH_SECRET;

$consumer_key = ETSY_KEY;
$consumer_secret = ETSY_SECRET;

$oauth = array(
                'oauth_consumer_key' => $consumer_key,
                'oauth_nonce' => time(),
                'oauth_signature_method' => 'HMAC-SHA1',
                'oauth_token' => $oauth_access_token,
                'oauth_timestamp' => time(),
                'oauth_version' => '1.0',
        );

$base_info = buildBaseString($url, 'GET', $oauth);

$composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
$oauth['oauth_signature'] = $oauth_signature;

$header = array(buildAuthorizationHeader($oauth), 'Expect:', 'Content-Type: application/json');
$options = array( CURLOPT_HTTPHEADER => $header,
                  CURLOPT_HEADER => false,
                  CURLOPT_URL => $url,
                  CURLOPT_CUSTOMREQUEST => 'GET',
                  CURLOPT_RETURNTRANSFER => true,
                  CURLOPT_SSL_VERIFYPEER => false);

$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);

#$return_data = json_decode($json);

print_r($json);


I tried to prescribe parameters in the link, I also tried to add $oauth to the array with data, but I still get an error ... I'm encountering OAuth for the first time.

Can you please tell me how to pass these parameters correctly? Or maybe there is some mistake in the code... I would be very grateful for the help!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question