How to authenticate POST to Discogs API using PHP / cURL

I have successfully used the https://gist.github.com/tonefolder/44191a29454f9059c7e6 example here to authenticate the user and save oauth_token and oauth_token_secret. Then I can execute authenticated GET requests using cURL.

I don't know how to make authenticated POST requests though.

I tried using the $ result ['header'] from a signed oauthObject this way:

$discogs_username = "'XXXX'";

$result = mysql_query("SELECT * FROM discogs WHERE username = $discogs_username;", $link);
if (mysql_num_rows($result)) {
    $discogs_details = mysql_fetch_array($result, MYSQL_ASSOC);
}


$signatures = array(
    'consumer_key'     => 'MyKey',
    'shared_secret'    => 'MySecret',
  'oauth_token'    => $discogs_details['oauth_token'],
  'oauth_token_secret'    => $discogs_details['oauth_token_secret']
);


$jsonquery = json_encode(array(
        'release_id' => '7608939',
        'price' => '18.00',
        'condition' => 'Mint',
        'sleeve_condition' => 'Mint',
        'status' => 'For Sale'
    )
);


$result = $oauthObject->sign(array(
        'path'      => $scope.'/marketplace/listings',
        'signatures'=> $signatures
    )
);


$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, 'MyDiscogsClient/0.1 +http://me.com');
curl_setopt($ch, CURLOPT_URL, $result['signed_url']);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Authorization:' . $result['header'])); 
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonquery);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = json_decode(curl_exec($ch), true);
curl_close($ch);

But I get the message [message] => You must be authenticated to access this resource.

I'm not sure if I am executing the CURLOPT_POSTFIELDS part correctly, but I can understand that one day I really can send an authenticated POST! Sorry if there are other errors with this - I don't often use cURL!

+4
1

, , . node, , $scope URL- API, https://api.discogs.com.

, OAuth , . , var_dump() $result sign():

array(5) {
/// ... snip ...
    'sbs' =>
  string(269) "GET&https%3A%2F%2Fapi.discogs.com%2Fmarketplace%2Flistings&oauth_consumer_key%XXXXX%26oauth_nonce%3DMOtWK%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1453937109%26oauth_token%XXXXX%26oauth_version%3D1.0"
}

, "OAuthSimple" , , , , , "".

, :

$result = $oauthObject->sign(
    array(
        'path' => $scope . '/marketplace/listings',
        'signatures' => $signatures,
    )
);

:

$result = $oauthObject->sign(
    array(
        'path' => $scope . '/marketplace/listings',
        'signatures' => $signatures,
        'action' => 'POST',
    )
);

API. , , - , _ Mint API, Mint (M).

, , , " ". .

, , GET. , , Guzzle Client, POST- , , , . , , , , .

, :)

+1

Source: https://habr.com/ru/post/1624846/


All Articles