2014-12-03 35 views
0

我使用的PECL的PHP​​的OAuth库试图建立一个OAuth服务器http://php.net/manual/en/book.oauth.phpPHP的Oauth 1.0提供商,签名不匹配

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

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); 
} 

我烧了好几个小时试图弄清楚,有人请帮助!

+0

问题肯定与OAuth提供者用于生成签名的uri信息有关。如果我把代码放在web根目录下的一个静态php文件中,它可以很好地工作。不过,我使用的是MVC框架,路由不是字面的。我已经尝试在checkOAuthRequest方法中手动设置uri和http方法,如下所示:'$ this-> provider-> checkOAuthRequest('https:// localhost/micro/v1/nappi',OAUTH_HTTP_METHOD_GET);'根据[ docs](http://php.net/manual/en/oauthprovider.checkoauthrequest.php),但它仍然不适用于框架环境。 – Lance 2014-12-05 09:00:09

回答

0

我终于设法解决了它,我的.htaccess文件有一个重写规则,它为我的框架处理_url参数。这个参数当然包含在服务器生成的签名中。我简单地指示OAuth提供程序忽略构造函数中的_url参数: $this->provider->setParam('_url',NULL);, 这就是所需要的,现在一切都运行良好。

+0

我面临同样的问题。虽然我没有看到额外的_url参数,但我仍然没有得到导致签名不匹配的原因。有没有什么办法可以看到服务器端的签名是什么以及用什么参数来生成签名? – satin 2015-10-14 16:27:10