【问题标题】:OAuth 1.0 one-legged client "HTTP 401 Unauthorized error" for PATCH method用于 PATCH 方法的 OAuth 1.0 单腿客户端“HTTP 401 未经授权的错误”
【发布时间】:2015-03-21 01:37:04
【问题描述】:

这是我第一次使用 OAuth,我在下面创建了部分工作的类!我关注了this manual

方法methodGet()methodPost() 工作正常,但是methodPatch() 返回“HTTP 401 未经授权的错误”。端点需要一个PATCH 请求方法,并且由于OAuth class 中有PATCHis no constant,我试图发送一个POST 请求并尝试用一个额外的X-Http-Method-Override 标头覆盖它,以便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());
        }
    }
}

【问题讨论】:

    标签: php api symfony oauth


    【解决方案1】:

    解决方法是使用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);
    }
    

    【讨论】:

      猜你喜欢
      • 2013-08-03
      • 2018-07-07
      • 1970-01-01
      • 2022-11-11
      • 2021-01-07
      • 1970-01-01
      • 2017-12-20
      • 2014-06-24
      • 2014-02-26
      相关资源
      最近更新 更多