【发布时间】:2018-01-12 07:45:46
【问题描述】:
我正在尝试在 PHP 中实现 trello api 的包装器。我在 OAuth 流程的最后一步中收到“无效签名”,我必须从 trello api 获取访问令牌。 只是通过这条消息,我无法调试我做错了什么。
基本上我所做的是......
- 发送获取请求令牌的请求 (https://trello.com/1/OAuthGetRequestToken)。这进展顺利。我返回了两个参数
oauth_token和oauth_token_secret作为响应。 - 然后我打开了 trello 授权页面 url (https://trello.com/1/OAuthAuthorizeToken),其中包含步骤 1 中的参数
oauth_token、应用程序名称和 localhost 的返回 url。这也很顺利。 Trello 使用参数oauth_token和oauth_verifier重定向到 localhost/callback。 - 在回调中,我终于发送了获取访问令牌的请求 (https://trello.com/1/OAuthGetAccessToken)。我从第 1 步添加了
oauth_token和oauth_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