2017-08-04 105 views
0

我試圖在PHP中實現trello API的包裝。我在OAuth流程的最後一步中遇到了「無效簽名」,我必須從trello API獲取令牌。 只是這個消息,我只是不能調試我做錯了什麼。訪問Trello API中的令牌時獲取無效簽名OAuth

基本上我所做的是...

  1. 發送用於提取請求令牌的請求(https://trello.com/1/OAuthGetRequestToken)。這進展順利。作爲迴應,我收到了兩個參數oauth_tokenoauth_token_secret
  2. 然後,我打開了來自步驟1的參數oauth_token的trello授權頁面url(https://trello.com/1/OAuthAuthorizeToken),app name和本地主機上的返回url。這也很好。 Trello重定向到本地/回調,參數oauth_tokenoauth_verifier
  3. 在回調中,我終於發送了獲取訪問令牌的請求(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))); 
+0

我將保持儘可能少的依賴關係的數量,這就是爲什麼香草php實現oauth – vikmalhotra

回答

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))); 
相關問題