How to get all commits of a single repository with OAUTH ?

Issue #31 resolved
Christian Loew created an issue

Hi, can u tell me how to do that? The first "getRepositories" is working, but the second doesn't. :C

<?php

// working 
private function getRepositories(){
        $user = $this->oauthBitbucket($this->bitbucket_consumer_key, $this->bitbucket_consumer_secret);
        $repo = $user->repositories()->get();
        return json_decode($repo->getContent());
    }

// not working, any better method doing it?

    private function getProjectCommits($teamName, $projectName){
        $this->oauthBitbucket($this->bitbucket_consumer_secret, $this->bitbucket_consumer_secret);
        $commits = new Bitbucket\API\Repositories\Commits();
        $commits->all($teamName, $projectName)->getContent();
        die(var_dump($commits));
    }

    private function oauthBitbucket($consumer_key, $consumer_secret){
        $oauth_params = array(
            'oauth_consumer_key'      => $this->bitbucket_consumer_key,
            'oauth_consumer_secret'   => $this->bitbucket_consumer_secret
        );

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

        return $user;
    }

Comments (8)

  1. Alexandru Guzinschi

    You have a small issue with your code:

    <?php
    private function getProjectCommits($teamName, $projectName){
        $this->oauthBitbucket($this->bitbucket_consumer_secret, $this->bitbucket_consumer_secret);
        $commits = new Bitbucket\API\Repositories\Commits();
        return json_decode($commits->all($teamName, $projectName)->getContent());
    }
    

    Also, the commits endpoint will return paginated response (30 results per page, if I remember correctly) and you need to build the logic yourself if you want to fetch everything.

    See #17 for more information.

  2. Christian Loew reporter

    The code provided above throws an unauthorized error. Here are the response headers, the content is an empty " " string:

    array (size=15)
      0 => string 'HTTP/1.1 401 UNAUTHORIZED' (length=25)
      1 => string 'Server: nginx/1.6.2' (length=19)
      2 => string 'Date: Wed, 05 Aug 2015 07:23:42 GMT' (length=35)
      3 => string 'Content-Type: text/html; charset=utf-8' (length=38)
      4 => string 'Content-Length: 0' (length=17)
      5 => string 'Connection: keep-alive' (length=22)
      6 => string 'X-Served-By: app19' (length=18)
      7 => string 'X-Render-Time: 0.0142071247101' (length=30)
      8 => string 'X-Static-Version: 5c1fc20162df' (length=30)
      9 => string 'Vary: Cookie' (length=12)
      10 => string 'X-Version: 5c1fc20162df' (length=23)
      11 => string 'ETag: "d41d8cd98f00b204e9800998ecf8427e"' (length=40)
      12 => string 'X-Request-Count: 474' (length=20)
      13 => string 'X-Frame-Options: SAMEORIGIN' (length=27)
      14 => string 'WWW-Authenticate: Basic realm="Bitbucket.org HTTP"' (length=50)
    

    I tried to authenticate via $user->setCredentials() and everything works fine, but i need it to work with oAuth. Are the oAuth logins restricted to $user->repositories() calls?

  3. Alexandru Guzinschi

    You get 401 error because you are trying to access data from a private repository and you didn't attached an OAuthListener to Commits in getProjectCommits(), like you did with User in oauthBitbucket().

    You could use a single point of entry to circumvent code repetition. I created a simple example for you, available here.

  4. Log in to comment