[英]PHP Oauth Invalid signature issue
我試圖從PHP開始使用OAuth 1.0,我遇到了奇怪的問題。 我創建了偽消費者,它根據規范生成簽名,並通過POST將其與使用過的參數一起發送給Provider。 消費者用途:
$oauth_consumer_key = '123';
$oauth_consumer_secret = '456';
$oauth_signature_method = 'HMAC-SHA1';
$oauth_timestamp = time();
$oauth_nonce = uniqid();
$oauth_version = '1.0';
$oauth_callback = 'http://localhost/oauth/callback';
$oauth = new OAuth($oauth_consumer_key, $oauth_consumer_secret);
$oauth->enableDebug();
$oauth_signature = $oauth->generateSignature('POST', $oauth_callback, array($oauth_consumer_key, $oauth_signature_method, $oauth_timestamp, $oauth_nonce, $oauth_version));
在供應商方面,一切似乎都按預期工作。 收到所有價值:
object(OAuthProvider)[1]
public 'consumer_key' => string '123' (length=3)
public 'consumer_secret' => string '456' (length=3)
public 'nonce' => string '5390610001c90' (length=13)
public 'token' => null
public 'token_secret' => null
public 'timestamp' => string '1401970944' (length=10)
public 'version' => string '1.0' (length=3)
public 'signature_method' => string 'HMAC-SHA1' (length=9)
public 'callback' => string 'http://localhost/oauth/callback' (length=31)
public 'request_token_endpoint' => boolean true
public 'signature' => string '8lNbnGTOen4TEOHS9KcpgCiBl+M=' (length=28)
但這是蜜月的結束 - 嘗試驗證簽名原因錯誤: signature_invalid 。 這是我在提供商方面使用的:
$provider = new OAuthProvider();
$provider->isRequestTokenEndpoint(true);
$provider->consumerHandler('lookupConsumer');
$provider->timestampNonceHandler('timestampNonceChecker');
try
{
$request_verified = $provider->checkOAuthRequest();
}
catch(OAuthException $e)
{
echo $provider->reportProblem($e);
}
以及我收到的問題報告:
oauth_problem=signature_invalid&debug_sbs=POST&http%3A%2F%2Flocalhost%2Foauth%2Fcustom_auth%2Frequest_token.php&oauth_callback%3Dhttp%253A%252F%252Flocalhost%252Foauth%252Fcallback%26oauth_consumer_key%3D123%26oauth_nonce%3D5390610001c90%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1401970944%26oauth_version%3D1.0
令我感到困惑的是,當我使用generateSignature作為相同的常量參數時(對於調試我將時間戳和隨機數設置為常量值),它每次都給我不同的值,就像是否還有一些我不知道的隨機元素。 作為驗證樣本 - hash_hmac沒有這樣的問題。
我錯過了某些東西,或者官方PHP OAuth實現是否存在問題( http://pecl.php.net/package/oauth )?
由於缺乏文檔,我現在已經用這個確切的問題摸不着頭腦了近一個星期但是這就解決了我的一切。
似乎OAuth
類自己的請求簽名。 我完成了與你完全相同的步驟但沒有用,但是一旦我刪除了所有參數,只是在我的網址上調用了fetch / getRequestToken就可以了。
我的代碼有效
$consumer_key = 'key';
$consumer_secret = 'secret';
$request_token_url = 'http://someurl.com/oauth/request-token';
$oauth = new OAuth($consumer_key, $consumer_secret);
$oauth->enableDebug(); //helpful debug
try {
$oauth->getRequestToken($request_token_url);
} catch (OAuthException $e) {
echo OAuthProvider::reportProblem($e); //easier to debug oauth exceptions
}
//this should hold the `request_token` and `request_token_secret` parameters for you to call getAccessToken
$response = json_decode($oauth->getLastResponse());
我在http://someurl.com/oauth/request-token
上設置了自己的提供程序,如下所示:
$provider = new OAuthProvider();
$provider->consumerHandler(array($this,'consumerHandler'));
$provider->timestampNonceHandler(array($this,'timestampNonceHandler'));
$provider->tokenHandler(array($this,'tokenHandler'));
$provider->setRequestTokenPath('/oauth/request-token');
try {
$request_verified = $provider->checkOAuthRequest();
} catch(OAuthException $e) {
echo $provider->reportProblem($e);
}
//provider now holds all the required timestamp, nonce, and signature
我希望這有所幫助,即使它是一年之后
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.