0
我試圖在PHP中實現trello API的包裝。我在OAuth流程的最後一步中遇到了「無效簽名」,我必須從trello API獲取令牌。 只是這個消息,我只是不能調試我做錯了什麼。訪問Trello API中的令牌時獲取無效簽名OAuth
基本上我所做的是...
- 發送用於提取請求令牌的請求(https://trello.com/1/OAuthGetRequestToken)。這進展順利。作爲迴應,我收到了兩個參數
oauth_token
和oauth_token_secret
。 - 然後,我打開了來自步驟1的參數
oauth_token
的trello授權頁面url(https://trello.com/1/OAuthAuthorizeToken),app name和本地主機上的返回url。這也很好。 Trello重定向到本地/回調,參數oauth_token
和oauth_verifier
。 - 在回調中,我終於發送了獲取訪問令牌的請求(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)));
我將保持儘可能少的依賴關係的數量,這就是爲什麼香草php實現oauth – vikmalhotra