【发布时间】:2015-03-21 01:37:04
【问题描述】:
这是我第一次使用 OAuth,我在下面创建了部分工作的类!我关注了this manual。
方法methodGet() 和methodPost() 工作正常,但是methodPatch() 返回“HTTP 401 未经授权的错误”。端点需要一个PATCH 请求方法,并且由于OAuth class 中有PATCH 的is 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());
}
}
}
【问题讨论】: