我试图使用OAuth 2.0MAC授权Paysera,但我总是收到401错误.你能给我指出我做错了什么吗?
这是一个规格:https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-http-mac-02
以下是我的代码:
private function _getTs() {
return time();
}
private function _getNonce() {
return uniqid();
}
private function _getMac($ts, $nonce, $method, $path, $host, $port, $key) {
return base64_encode(hash_hmac("sha256", implode("\n", Array(
$ts,
$nonce,
$method,
$path,
$host,
$port,
"",
)), $key, true));
}
private function _getHeader($id, $method, $path, $host, $port, $key) {
$ts = $this->_getTs();
$nonce = $this->_getNonce();
$mac = $this->_getMac($ts, $nonce, $method, $path, $host, $port, $key);
$params = Array(
"id" => $id,
"ts" => $ts,
"nonce" => $nonce,
"mac" => $mac,
);
$parts = [];
foreach ($params as $name => $value) {
$parts[] = $name . '="' . $value . '"';
}
return "MAC " . implode(", ", $parts);
}
public function test() {
$httpClient = new \Bitrix\Main\Web\HttpClient();
$httpClient->setHeader("Authorization", $this->_getHeader(
KEY_ID,
"GET",
"/rest/v1/post-offices",
"delivery-api.paysera.com",
443,
KEY,
));
$httpClient->query("GET", "https://delivery-api.paysera.com/rest/v1/post-offices");
return $httpClient->getResult();
}