2013-10-06 45 views
1

時,我一直在努力實現使用OAuthv1.a和比特鬥簡單的認證流程。當我使用先前提供的驗證器和oauth_token發出訪問令牌請求時,就會出現我的問題。我總是被給出400錯誤,但沒有真正指出原因。錯誤的請求提交狂飲POST來位鬥訪問令牌端點

Client error response 
[status code] 400 
[reason phrase] BAD REQUEST 
[url] https://bitbucket.org/api/1.0/oauth/access_token?oauth_consumer_key=<snip>&oauth_nonce=fba24cfb3147ca7d32b3924fad43fd509bbb9bc1&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1381034857&oauth_token=fFz369NUmCHNyn7PGj&oauth_verifier=6724267371&oauth_version=1.0&oauth_signature=1H7%2Bvx0fVh2Sj%2FcDAE2QzkTx8us%3D 

我使用狂飲內OauthPlugin類,如文檔中描述的建立簽署參數和提交POST請求。有沒有人有任何其他OAuthv1提供者或位桶專門?

$client = new Client('https://bitbucket.org/api/1.0/'); 

    $oauth = new OauthPlugin(array(
     'request_method' => OauthPlugin::REQUEST_METHOD_QUERY, 
     'consumer_key' => Config::get('oauthv1.key'), 
     'token' => Input::get('oauth_token'), 
     'verifier' => Input::get('oauth_verifier') 
     ) 
    ); 

    $client->addSubscriber($oauth); 
    $client->post('oauth/access_token')->send(); 

回答

5

即使到位桶API文檔沒有提到它,在調用的OAuth/ACCESS_TOKEN端點也需要CONSUMER_SECRET和oauth_token_secret。消費者機密是在您創建應用時由Bitbucket生成的,並應存儲在您的配置中。您可以將oauth_token_secret從調用響應中獲取到oauth/request_token。只需將它保存在會話中,以便在獲取訪問令牌時使用它。

請求請求令牌:

$client = new Client('https://bitbucket.org/api/1.0'); 
$oauth = new OauthPlugin(array(
    'consumer_key' => $app['bitbucket.key'], 
    'consumer_secret' => $app['bitbucket.secret'], 
    'callback'  => 'http://mysite.local/callback',   
)); 
$client->addSubscriber($oauth); 
$response = $client->post('oauth/request_token')->send(); 

// Parse the response 
parse_str($response->getBody(), $result); 

// Save the token secret in the session 
$app['session']->set('oauth_token_secret', $result['oauth_token_secret']); 

// Redirect to Bitbucket to authorize the application 
return $app->redirect(sprintf('https://bitbucket.org/api/1.0/oauth/authenticate?oauth_token=%s', $result['oauth_token'])); 

請求的接入令牌:

$token  = $app['request']->get('oauth_token'); 
$verifier = $app['request']->get('oauth_verifier'); 
$tokenSecret = $app['session']->get('oauth_token_secret'); 

$client = new Client('https://bitbucket.org/api/1.0'); 
$oauth = new OauthPlugin(array(
    'consumer_key' => $app['bitbucket.key'], 
    'consumer_secret' => $app['bitbucket.secret'], 
    'token'   => $token, 
    'token_secret' => $tokenSecret, 
    'verifier'  => $verifier, 
)); 
$client->addSubscriber($oauth); 
$client->post('oauth/access_token')->send(); 

// Parse the response 
$response = parse_str($response->getBody(), $result); 

// Get the access token 
$accessToken = $result['oauth_token']; 
相關問題