【问题标题】:PHP Oauth Invalid signature issuePHP Oauth 签名无效问题
【发布时间】:2014-06-05 12:58:18
【问题描述】:

我尝试从 PHP 中的 OAuth 1.0 开始,但遇到了奇怪的问题。我创建了伪消费者,它根据规范生成签名,并通过 POST 将使用的参数发送给提供者。消费者用途:

$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。这是我在 Providers 端使用的:

$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) 有问题?

【问题讨论】:

    标签: php oauth


    【解决方案1】:

    由于缺乏文档,我一直在为这个确切的问题挠头将近一个星期,但这为我解决了所有问题。

    似乎OAuth 类有自己的请求签名。我已经完成了与您完全相同的步骤,但无济于事,但是一旦我删除了所有参数并在我的 url 上调用了 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
    

    我希望这会有所帮助,即使是一年之后

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-26
      • 1970-01-01
      • 2013-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多