【发布时间】:2016-04-24 01:59:13
【问题描述】:
我已成功使用此处的示例https://gist.github.com/tonefolder/44191a29454f9059c7e6 来验证用户并存储 oauth_token 和 oauth_token_secret。然后我可以使用 cURL 发出经过身份验证的 GET 请求。
我不知道如何发出经过身份验证的 POST 请求。
我尝试过以这种方式使用已签名 oauthObject 中的 $result['header']:
$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);
但我得到'[message] => 您必须进行身份验证才能访问此资源。'
我不确定我是否正确地执行了 CURLOPT_POSTFIELDS 部分,但是一旦我真正能够发送经过身份验证的 POST,我就可以弄清楚!抱歉,如果这还有更多错误 - 我不经常使用 cURL!
【问题讨论】:
-
我也在 Discogs API 论坛上问过这个问题link,但没有人回复。我在这里要求的东西真的很晦涩吗?
-
我看到 discogs 在这里有很好的 api 文档:discogs.com/developers 并且在快速入门中:discogs.com/developers/#page:home,header:home-quickstart,存在 PHP 客户端 API 库:github.com/ricbra/php-discogs-api,您可以尝试使用它进行通信与他们一起创造服装方法
-
谢谢,但该客户端似乎只支持 GET 请求而不是 POST 请求
-
您只发布了部分代码,或者这是您正在使用的完整代码?
-
没有太多多余的东西,只是创建了签名数组,我会编辑它。
标签: php authentication curl oauth discogs-api