Php 访问Trello API OAuth中的令牌时获取无效签名
我试图在PHP中实现TrelloAPI的包装器。在OAuth过程的最后一步中,我得到了“无效签名”,我必须从TrelloAPI获得访问令牌。 仅凭这条信息,我无法调试我做错了什么 基本上我做的是Php 访问Trello API OAuth中的令牌时获取无效签名,php,authentication,oauth,trello,Php,Authentication,Oauth,Trello,我试图在PHP中实现TrelloAPI的包装器。在OAuth过程的最后一步中,我得到了“无效签名”,我必须从TrelloAPI获得访问令牌。 仅凭这条信息,我无法调试我做错了什么 基本上我做的是 发送获取请求令牌()的请求。这一切进展顺利。作为响应,我得到了两个参数oauth\u-token和oauth\u-token\u-secret 然后,我打开了trello授权页面url(),其中包含步骤1中的参数oauth_token,app name和localhost上的返回url。这也进行得很顺利
oauth\u-token
和oauth\u-token\u-secret
oauth_token
,app name和localhost上的返回url。这也进行得很顺利。Trello使用参数oauth\u令牌
和oauth\u验证器
重定向到本地主机/回调oauth\u令牌
&oauth\u令牌
,在第2步中添加了oauth\u验证器
,并使用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)));
在生成基本字符串或发送实际请求时,URL参数中不应包含oauth令牌机密。令牌密钥仅用作哈希密钥的一部分。请参见下面的修改代码:
$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)));
我将尽量减少依赖项的数量,这就是为什么oauth的普通php实现