PATCH 方法的 OAuth 1.0 独脚客户端 "HTTP 401 Unauthorized error"

OAuth 1.0 one-legged client "HTTP 401 Unauthorized error" for PATCH method

这是我第一次使用 OAuth,我创建了下面的 class,部分有效!我关注了 this manual.

方法 methodGet()methodPost() 工作正常,但是 methodPatch() returns "HTTP 401 Unauthorized error"。端点需要一个 PATCH 请求方法,因为在幕后有 is no constant for PATCH in OAuth class, I'm trying to send a POST request and trying to override it with an extra X-Http-Method-Override header so that it becomes 一个 PATCH 方法(可能不是!!!)。就是这个问题,我无法修补它!

因为它很可能与 PATCH 相关(GET 和 POST 工作正常),有人知道解决方案还是我遗漏了其他东西?

注意:我可以确认端点工作正常,所以那边没有问题。

提前致谢

use Exception;
use OAuth;
use OAuthException;

class ApiClient
{
    // End-point accepts GET request - This works fine
    public function methodGet()
    {
        return $this->call(
            OAUTH_HTTP_METHOD_GET,
            array('id' => 123)
        );
    }

    // End-point accepts POST request - This works fine
    public function methodPost()
    {
        return $this->call(
            OAUTH_HTTP_METHOD_POST,
            array('name' => 'inanzzz')
        );
    }

    // End-point accepts PATCH request - This returns HTTP 401 Unauthorized
    public function methodPatch()
    {
        return $this->call(
            OAUTH_HTTP_METHOD_POST,
            array('id' => 123, 'name' => 'inanzzz123'),
            ['X-Http-Method-Override' => 'PATCH']
        );
    }

    private function call($method, $params = array(), $headers = array())
    {
        try {
            $oAuth = new OAuth('api_key_goes_here', 'api_secret_goes_here');
            $oAuth->setNonce(md5(uniqid(mt_rand(), true)));
            $oAuth->setTimestamp(time());
            $oAuth->setVersion('1.0');
            $oAuth->fetch(
               'http://api.domain.com/1/products/service.json',
               $params, $method, $headers
            );

            return json_decode($oAuth->getLastResponse(), true);
        } catch (OAuthException $e) {
            throw new Exception($e->getMessage(), $e->getCode());
        }
    }
}

解决方案是使用 Guzzle Client,方法如下:

注意:$authHeader 包含 $oauth->getRequestHeader(...);,因此您可以生成它并将其传递给方法。

private function call($uri, $method, $authHeader, array $payload = [])
{
    try {
        $client = new Client();
        $request = $client->createRequest($method, $uri);
        $request->addHeader('Authorization', $authHeader);
        $request->addHeader('Content-Type', 'application/json');
        $request->setBody(Stream::factory(json_encode($payload)));
        $response = $client->send($request);
    } catch (RequestException $e) {
        $message = $e->hasResponse()
            ? $e->getResponse()
            : 'An unknown error occurred while trying to process your request.';

        throw new Exception($message);
    }

    return json_decode($response->getBody(), true);
}