OAuth的1.0独腿客户端和QUOT; HTTP 401错误未经授权"对于PATCH方法 [英] OAuth 1.0 one-legged client "HTTP 401 Unauthorized error" for PATCH method
问题描述
这是我第一次使用OAuth和我创建低于该部分工程类!我跟着本手册。
方法 methodGet()
和 methodPost()
做工精细但 methodPatch()
返回HTTP 401未经授权错误。终点需要一个 PATCH
请求方法和由于的是 PATCH
中的http://php.net/manual/en/book.oauth.php相对=nofollow的> OAuth的类,我试图发送一个 POST
请求,并尝试使用一个额外的覆盖它 X- HTTP的方法,覆盖
头让成为在 PATCH
方法(可能是没有!)。这就是问题所在,我不能修补它!
由于它极有可能与 PATCH做
(GET和POST做工精细),没有人知道一个解决方案,它还是我失去了什么东西?
请注意:我可以证实,终点正常工作所以在那边没有问题
在此先感谢
使用异常;
使用OAuth;
使用OAuthException;类ApiClient
{
//终点接受GET请求 - 这工作正常
公共职能methodGet()
{
返回$这个 - >调用(
OAUTH_HTTP_METHOD_GET,
阵列('ID'=> 123)
);
} //终点接受POST请求 - 这工作正常
公共职能methodPost()
{
返回$这个 - >调用(
OAUTH_HTTP_METHOD_POST,
阵列('名'=>'inanzzz')
);
} //终点接受PATCH请求 - 这将返回HTTP 401未授权
公共职能methodPatch()
{
返回$这个 - >调用(
OAUTH_HTTP_METHOD_POST,
阵列('ID'=> 123,'名'=>'inanzzz123'),
['X-HTTP-方法-覆盖'=> '补丁']
);
} 私有函数调用($方法,$ PARAMS =阵列(),$头=阵列())
{
尝试{
$ =的OAuth新的OAuth('api_key_goes_here','api_secret_goes_here');
$ oAuth-> setNonce(MD5(uniqid(mt_rand(),TRUE)));
$ oAuth->的setTimestamp(时间());
$ oAuth-> setVersion('1.0');
$ oAuth->取(
http://api.domain.com/1/products/service.json',
$参数,可以$方法,$头
); 返回json_de code($ oAuth->使用getLastResponse(),TRUE);
}赶上(OAuthException $ E){
抛出新的异常($ E-GT&;的getMessage(),$ E-GT&;得到code());
}
}
}
解决方案是使用客户端狂饮所以方法如下:
请注意: $ authHeader
持有 $ oauth-> getRequestHeader(...);
这样你就可以生成它并把它传递给方法。
私有函数调用($ URI,$方法,$ authHeader,数组$有效载荷= [])
{
尝试{
$客户端=新客户();
$请求= $客户端 - >的createRequest($方法,$ URI);
$请求 - >和addHeader('授权',$ authHeader);
$请求 - >和addHeader('内容类型,应用/ JSON');
$请求 - > setBody(流::工厂(json_en code($有效载荷)));
$响应= $客户 - >发($请求);
}赶上(RequestException $ E){
$消息= $ E-GT&; hasResponse()
? $ E-GT&; GETRESPONSE()
:尝试处理您的请求时出现未知错误。'; 抛出新的异常($消息);
} 返回json_de code($响应 - > getBody(),TRUE);
}
This is the first time I'm using OAuth and I created the class below which partially works! I followed this manual.
Methods methodGet()
and methodPost()
work fine however methodPatch()
returns "HTTP 401 Unauthorized error". End-point expects a PATCH
request method and since there 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 a PATCH
method behind the scene (may be not!!!). That's the problem, I cannot PATCH it!
As it is highly likely to do with PATCH
(GET and POST work fine), does anyone know a solution to it or am I missing something else?
Note: I can confirm that the end-point works fine so there is no problem at that side.
Thanks in advance
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());
}
}
}
Solution was to use Guzzle Client so the method is below:
Note: $authHeader
holds $oauth->getRequestHeader(...);
so you can generate it and pass it to the method.
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);
}
这篇关于OAuth的1.0独腿客户端和QUOT; HTTP 401错误未经授权"对于PATCH方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!