【问题标题】:Getting Invalid Signature when accessing tokens in Trello API OAuth在 Trello API OAuth 中访问令牌时获取无效签名
【发布时间】:2018-01-12 07:45:46
【问题描述】:

我正在尝试在 PHP 中实现 trello api 的包装器。我在 OAuth 流程的最后一步中收到“无效签名”,我必须从 trello api 获取访问令牌。 只是通过这条消息,我无法调试我做错了什么。

基本上我所做的是......

  1. 发送获取请求令牌的请求 (https://trello.com/1/OAuthGetRequestToken)。这进展顺利。我返回了两个参数 oauth_tokenoauth_token_secret 作为响应。
  2. 然后我打开了 trello 授权页面 url (https://trello.com/1/OAuthAuthorizeToken),其中包含步骤 1 中的参数 oauth_token、应用程序名称和 localhost 的返回 url。这也很顺利。 Trello 使用参数 oauth_tokenoauth_verifier 重定向到 localhost/callback。
  3. 在回调中,我终于发送了获取访问令牌的请求 (https://trello.com/1/OAuthGetAccessToken)。我从第 1 步添加了oauth_tokenoauth_token_secret,从第2 步添加了oauth_verifier,并使用HMAC-SHA1 方法添加了签名。当我收到带有“无效签名”消息的 500 内部错误时,这出了问题!!!

有人知道可能出了什么问题吗?

这是我在回调中使用的代码。

$nonce = md5(mt_rand());
$timestamp = time();

$oauth_signature_base = 'GET&'.
    rawurlencode('https://trello.com/1/OAuthGetAccessToken').'&'.
    rawurlencode(implode('&', [
        'oauth_consumer_key='.rawurlencode('CONSUMER_KEY_HERE'),
        'oauth_nonce='.rawurlencode($nonce),
        'oauth_signature_method='.rawurlencode('HMAC-SHA1'),
        'oauth_timestamp='.rawurlencode($timestamp),
        'oauth_token='.rawurlencode('OAUTH_TOKEN_HERE'),
        'oauth_token_secret='.rawurlencode('OAUTH_TOKEN_SECRET_HERE'),
        'oauth_verifier='.rawurlencode('OAUTH_VERIFIER_HERE'),
        'oauth_version='.rawurlencode('1.0')
        ]));

$signature = base64_encode(hash_hmac('sha1', $oauth_signature_base, 'CONSUMER_SECRET_HERE&', true));

$params = [
    'oauth_consumer_key='.rawurlencode('CONSUMER_KEY_HERE'),
    'oauth_nonce='.rawurlencode($nonce),
    'oauth_signature_method='.rawurlencode('HMAC-SHA1'),
    'oauth_timestamp='.rawurlencode($timestamp),
    'oauth_token='.rawurlencode('OAUTH_TOKEN_HERE'),
    'oauth_token_secret='.rawurlencode('OAUTH_TOKEN_SECRET_HERE'),
    'oauth_verifier='.rawurlencode('OAUTH_VERIFIER_HERE'),
    'oauth_version='.rawurlencode('1.0'),
    'oauth_signature='.rawurlencode($signature)
];
file_get_contents(sprintf('%s?%s', 'https://trello.com/1/OAuthGetAccessToken', implode('&', $params)));

【问题讨论】:

  • 我将尽可能减少依赖项的数量,这就是为什么 oauth 的 vanilla php 实现

标签: php authentication oauth trello


【解决方案1】:

在生成基本字符串或发送实际请求时,不应将 oauth 令牌秘密包含在 URL 参数中。令牌秘密仅用作散列密钥的一部分。请参阅下面的修改代码:

$nonce = md5(mt_rand());
$timestamp = time();

$oauth_signature_base = 'GET&'.
rawurlencode('https://trello.com/1/OAuthGetAccessToken').'&'.
rawurlencode(implode('&', [
    'oauth_consumer_key='.rawurlencode('CONSUMER_KEY_HERE'),
    'oauth_nonce='.rawurlencode($nonce),
    'oauth_signature_method='.rawurlencode('HMAC-SHA1'),
    'oauth_timestamp='.rawurlencode($timestamp),
    'oauth_token='.rawurlencode('OAUTH_TOKEN_HERE'),
    'oauth_verifier='.rawurlencode('OAUTH_VERIFIER_HERE'),
    'oauth_version='.rawurlencode('1.0')
    ]));

//token secret should be (singly) URL encoded if not already
$signature = base64_encode(hash_hmac('sha1', $oauth_signature_base, 'CONSUMER_SECRET_HERE&TOKEN_SECRET_HERE', true));

$params = [
'oauth_consumer_key='.rawurlencode('CONSUMER_KEY_HERE'),
'oauth_nonce='.rawurlencode($nonce),
'oauth_signature_method='.rawurlencode('HMAC-SHA1'),
'oauth_timestamp='.rawurlencode($timestamp),
'oauth_token='.rawurlencode('OAUTH_TOKEN_HERE'),
'oauth_verifier='.rawurlencode('OAUTH_VERIFIER_HERE'),
'oauth_version='.rawurlencode('1.0'),
'oauth_signature='.rawurlencode($signature)
];
file_get_contents(sprintf('%s?%s', 'https://trello.com/1/OAuthGetAccessToken', implode('&', $params)));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-28
    • 2018-09-24
    • 1970-01-01
    • 2011-04-25
    • 1970-01-01
    • 2019-11-09
    • 2018-04-14
    • 1970-01-01
    相关资源
    最近更新 更多