加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP Oauth无效的签名问题

发布时间:2020-12-13 22:52:45 所属栏目:PHP教程 来源:网络整理
导读:我试图从 PHP开始使用OAuth 1.0,我遇到了奇怪的问题.我创建了伪消费者,它根据规范生成签名,并通过POST将其与使用过的参数一起发送给Provider.消费者用途: $oauth_consumer_key = '123';$oauth_consumer_secret = '456';$oauth_signature_method = 'HMAC-SHA
我试图从 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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读