Authentication issue (Unauthorized)

Issue #28 resolved
Dragosh Mocrii created an issue

Trying to get the repositories of a user, but Bitbucket returns code 401 Unauthorized. Here's the code I use:

$oauth_params = array(
    'oauth_consumer_key'    => $plugin->get_option( 'bitbucket_consumer_key' ),
    'oauth_consumer_secret' => $plugin->get_option( 'bitbucket_consumer_secret' )
);

$user = new Bitbucket\API\User;
$user->getClient()->addListener(
    new Bitbucket\API\Http\Listener\OAuthListener( $oauth_params )
);

// now you can access protected endpoints as consumer owner
$response = $user->repositories()->overview();

print_r( $response );

Consumer key and consumer secret are properly set. Any ideas what is wrong with this piece of code? Thank you!

Comments (11)

  1. Alexandru Guzinschi

    I can't reproduce this issue with version 0.6.1 and the code that you provided.

    Can you double check your OAuth consumer credentials ? Also try to debug $plugin->get_option() and make sure that returns expected credentials.

  2. Dragosh Mocrii reporter

    @vimishor

    I have checked that both the consumer key and consumer secret keys are correctly feeded to the OAuthListener constructor. But still, Bitbucket returns the following:

    Buzz\Message\Response Object ( [protocolVersion:Buzz\Message\Response:private] => [statusCode:Buzz\Message\Response:private] => [reasonPhrase:Buzz\Message\Response:private] => [headers:Buzz\Message\AbstractMessage:private] => Array ( [0] => HTTP/1.1 401 UNAUTHORIZED [1] => Server: nginx/1.6.2 [2] => Date: Mon, 18 May 2015 08:47:57 GMT [3] => Content-Type: text/html; charset=utf-8 [4] => Content-Length: 0 [5] => Connection: keep-alive [6] => X-Served-By: app-104 [7] => X-Render-Time: 0.00844407081604 [8] => Content-Language: en [9] => X-Static-Version: 9ac04f7b4548 [10] => Vary: Authorization, Accept-Language, Cookie [11] => bbuserid: [12] => X-Version: e4b83af8fc08 [13] => ETag: "d41d8cd98f00b204e9800998ecf8427e" [14] => X-Request-Count: 146 [15] => X-View-Name: bitbucket.apps.api.v10.handlers.UserRepositoryOverviewHandler [16] => X-Frame-Options: SAMEORIGIN [17] => WWW-Authenticate: Basic realm="Bitbucket.org HTTP" [18] => bbusername: [19] => X-Usage-User-Time: 0.008953 [20] => X-Usage-System-Time: 0.0 [21] => X-Usage-Input-Ops: 0 [22] => X-Usage-Output-Ops: 0 ) [content:Buzz\Message\AbstractMessage:private] => )
    

    Do I need to make any actions prior to how the code above is used, besides creating a new pair of consumer/secret keys?

    Thank you!

  3. Alexandru Guzinschi

    You will get that type of response when you try to use wrong credentials.

    Try to remove $plugin reference and feed OAuth consumer credentials directly, just to give it a test:

    $oauth_params = array(
        'oauth_consumer_key'    => '<consumer_key>',
        'oauth_consumer_secret' => '<consumer_secret>'
    );
    
    $user = new Bitbucket\API\User;
    $user->getClient()->addListener(
        new Bitbucket\API\Http\Listener\OAuthListener( $oauth_params )
    );
    
    // now you can access protected endpoints as consumer owner
    $response = $user->repositories()->overview();
    
    var_dump(json_decode($response->getContent(), true));
    

    You can get your consumer credentials from this page, after you create a new Consumer.

  4. Dragosh Mocrii reporter

    Thank you for your reply.

    I have tried the code you provided above, with credentials fed directly as array, however the var_dump function returns NULL.

    I noticed that all calls to User\Repositories endpoints would fail with this API wrapper. For example the following works properly:

    $user = new Bitbucket\API\User;
    $user->getClient()->addListener(
        new Bitbucket\API\Http\Listener\OAuthListener( $oauth_params )
    );
    
    // now you can access protected endpoints as consumer owner
    $response = $user->get();
    
    print_r( $response );
    
  5. Dragosh Mocrii reporter

    @vimishor

    I think I found the problem. Though not sure what exactly is wrong, I think the problem resides in the childFactory method (more specifically, it could be when listeners are transferred to the child instance). I have instantiated directly Bitbucket\API\User\Repositories and added the listener directly to it, and it works this way.

  6. Alexandru Guzinschi

    Tagged 0.6.2 which contains a fix from f5dd7ec for this issue.

    Give it a test and please confirm if the issue is fixed or not.

  7. Log in to comment