2017-08-04 6 views
0

PHPでtrello apiのラッパーを実装しようとしています。私はtrello apiからアクセストークンを取得しなければならないOAuthプロセスの最後のステップで "Invalid signature"を取得しています。 このメッセージだけで、私は間違っていることをデバッグできません。Trello APIのトークンにアクセスすると無効なシグネチャを取得するOAuth

は基本的に私がやったことだった...

  1. リクエストトークン(https://trello.com/1/OAuthGetRequestToken)をフェッチする要求を送信します。これはうまくいった。応答で2つのパラメータoauth_tokenoauth_token_secretが返されました。
  2. 次に、ステップ1のパラメータoauth_token、アプリケーション名、localhostの戻りURLを持つtrello認証ページのURL(https://trello.com/1/OAuthAuthorizeToken)を開きました。これもうまくいった。 Trelloは、パラメータoauth_tokenoauth_verifierでローカルホスト/コールバックにリダイレクトされました。
  3. コールバックで、最終的にアクセストークン(https://trello.com/1/OAuthGetAccessToken)を取得する要求を送信しました。私はステップ1のoauth_token & oauth_token_secretを追加し、ステップ2のoauth_verifierHMAC-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

私は可能な限り少ない依存性の数を保つつもりです。その理由は、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))); 
関連する問題