2013-02-14 88 views
2

我正在使用第三方服务,这需要我通过OAuth1进行身份验证才能发出请求。我可以使用GET成功认证并获取大部分电话的数据,但有些电话需要POST,这就是问题所在。为了验证我使用下面的:不要将OAuth传递给POST变量 - PHP OAuth Pecl库

$oauth = new OAuth(MY_KEY,MY_SECRET); 
$oauth->setNonce(rand());  
$oauth->setToken('',''); 

那么对于一个GET调用我做类似如下:

$array = array(
    'partnerId'=>'1234' 
    ); 

$call = $oauth->fetch("https://domain.co.uk/api/getInfo/",$array); 
$data = $oauth->getLastResponse(); 

这所有的作品完美,我可以打印出$数据

但是与POST要求:

$oauth = new OAuth(MY_KEY,MY_SECRET); 
$oauth->setNonce(rand());  
$oauth->setToken('',''); 
$oauth->enableDebug(); 
$oauth->setAuthType(OAUTH_AUTH_TYPE_AUTHORIZATION); 

$array = array(
    'rid' => "$restaurantId", 
    'datetime' => "$datetime", 
    'partysize' => $covers, 
    'timesecurityID' => "$securityId", 
    'resultskey' => "$resultskey" 
); 

$call = $oauth->fetch("https://domain.co.uk/api/booking/?pid=1234&st=0",$array,OAUTH_HTTP_METHOD_POST); 
$data = $oauth->getLastResponse(); 

我不断收到错误:Invalid Consumer Signature

说起自己的科技人,他建议

Your sbs value indicates that you’re signing with all of the POST parameters, whereas you only need to sign with the query string. In this case it would be “pid=1234&st=0”. Unfortunately, I’m not familiar with the PHP libs and don’t have any recommendations on how to alter the behavior.

和还提到了PHP实现共同以前的问题是:

  1. PHP的HTTP LIB将下降查询字符串,一旦方法是 从GET更改为POST。
  2. PHP oAuth lib将使用发布数据 来签署请求,而不是查询字符串或两者。

如果我倾倒出来的头,我得到:

[sbs] => POST&https%3A%2F%2Fdomain.co.uk%2Fapi%2Fbooking%2F&datetime%3D2013-02-21T10%253A30%253A00%26oauth_consumer_key%3DMySiteV3TT%26oauth_nonce%3D1213111151%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1360835965%26oauth_token%3D%26oauth_version%3D1.0%26partysize%3D2%26pid%3D1531%26resultskey%3DfoqgEnYK%25252bIzRd6BV3T8eGQ%25253d%25253d%26rid%3D31852%26st%3D0%26timesecurityID%3D349367809 
[headers_sent] => POST /api/booking/?pid=1234&st=0 HTTP/1.1 

它看起来就像是与文章的其余发送OAuth的数据,我只想这个发送的授权头(它还派)

Authorization: OAuth oauth_consumer_key="MySite",oauth_signature_method="HMAC-SHA1",oauth_nonce="1772854358",oauth_timestamp="1360768712",oauth_version="1.0",oauth_token="",oauth_signature="2%2B7xb%2BJ5cdbUDC5UHfsdfsNpFM1pE%3D" 

所以我觉得我需要从发布请求剥离OAuth的数据,但把它作为一个授权头,但就是无法找到神奇方式来做到这一点!

回答

0

我见过这个。在取(),尝试URL编码您的$阵列,并把它当作形式的字符串:

rid=[restaurantId]&datetime=[datetime]&... 

也给头(读取的最后一个参数()):

Content-Type: application/x-www-form-urlencoded