PHP Oauth 1.0提供程序,签名不匹配

时间:2014-12-03 10:30:03

标签: php oauth

我正在尝试使用pecl-php oauth库设置Oauth服务器http://php.net/manual/en/book.oauth.php

此代码假定客户端已经收到用户验证的访问令牌,为简单起见,我没有包含任何数据库调用,并将硬编码的匹配值放入我的客户端和提供程序。

Class OauthVerify
{
   private static $consumer_secret = 'f63ed7f7a8899e59d3848085c9668a0d';
   private static $token_secret = '72814e6059441037152eecef2e8559a748b84259';
   private $provider;

   public function __construct()
{
    $this->provider = new OAuthProvider();
    $this->provider->consumerHandler(array($this,'consumerHandler'));
    $this->provider->timestampNonceHandler(array($this,'timestampNonceHandler'));
    $this->provider->tokenHandler(array($this,'checkAccessToken'));

} 

  //Check the client request

   public function checkRequest()
{
    try {
        $this->provider->checkOAuthRequest();
    } catch (Exception $Exception) {
        return OAuthProvider::reportProblem($Exception);

    }
    return true;
}


   public static function timestampNonceHandler($Provider)
{
     //I'm leaving out this logic now, to keep it simple and for testing purposes
     return OAUTH_OK;
}

  public static function consumerHandler($Provider)
{
    //I'm leaving out this logic now, to keep it simple and for testing purposes

    $Provider->consumer_secret = self::$consumer_secret;
    return OAUTH_OK;
}

  public static function checkAccessToken($Provider)
{
    $Provider->token_secret = self::$token_secret;
    return OAUTH_OK;
}
}

上面的代码应该给我验证Oauth请求所需的准系统。 在执行任何特定路由之前,我调用$ OauthVerify-> checkRequest()方法,该方法检查客户端请求是否有效,但是服务器不断抛出“签名不匹配”错误。我不认为问题出在客户端,因为我已经尝试了postman(用于chrome)和PHP实现,并且它们都生成相同的签名。不过,我感兴趣的是我的客户电话。

$consumer_key = '87d6d61e87f0e30d8747810ae40041d1';
$consumer_secret = 'f63ed7f7a8899e59d3848085c9668a0d';
$token= 'b9d55b3ec4b755d3fe25d7a781da1dfd044b5155';
$token_secret = '72814e6059441037152eecef2e8559a748b84259';
$timestamp = '1417515075';
$nonce = '9QV4rn';
$version = '1.0';

$method = 'GET';
$url = 'https://localhost/micro/v1/nappi';

try {
     $oauth = new OAuth($consumer_key, $consumer_secret, OAUTH_SIG_METHOD_HMACSHA1,       OAUTH_AUTH_TYPE_URI);
     $oauth->enableDebug();
     $oauth->disableSSLChecks();
     $oauth->setNonce($nonce);
     $oauth->setTimestamp($timestamp);
     $oauth->setToken($token, $token_secret);
     $oauth->setVersion($version);
     $oauth->fetch("$url");
     $json = json_decode($oauth->getLastResponse());
     print_r($json);
 }
    catch(OAuthException $E) {
        print_r($E);
 }

我已经烧了好几个小时试图解决这个问题,有人请求帮助!

1 个答案:

答案 0 :(得分:0)

我终于设法解决了它,我的.htaccess文件有一个重写规则,正在处理我的框架的_url参数。此参数当前包含在服务器生成的签名中。我只是指示OAuth提供程序忽略构造函数中的_url参数: $this->provider->setParam('_url',NULL);, 这就是一切,现在一切都很完美。