sending issue to bitbucket using oauth via bitbucket api

耗尽温柔 提交于 2019-12-13 17:58:14

问题


i have read this related question; Request OAuth token from BitBucket

in that question above , it uses curl . but there must be a way to do it with gentlero-api because it has php class in it about oauth.

    $bb_user = 'myuser_name';
    $bb_pass = 'mypasss';
    $account_name = 'account_name';    
    $repo_slug = 'repo_name';

    $issue = new  issues();
    $issue->setCredentials( new Basic($bb_user, $bb_pass) );   

    // iwanna do something like.  but how??
    //  $issue->setCredentials( new Oauth($key, $secret) );     


    $issue->create($account_name, $repo_slug, array(
        'title'     => 'konu',
        'content'   => 'içerik metin 123123',
        'kind'      => 'proposal',
        'priority'  => 'blocker'
        ));

i want to do oauth like that simple but. i coudnt find any good resouce.

edit:

//i did it with basic auth like this. https://github.com/gentlero/bitbucket-api/blob/master/docs/repositories/issues.md

//prepare 
$issue = new Bitbucket\API\Repositories\Issues();
$issue->setCredentials( new Bitbucket\API\Authentication\Basic($bb_user, $bb_pass) );

//create new issue
$issue->create($account_name, $repo_slug, array(
    'title'     => 'dummy title',
    'content'   => 'dummy content',
    'kind'      => 'proposal',
    'priority'  => 'blocker'
));

and there is this too; this code does oauth.

// OAuth 1-legged example
// You can create a new consumer at:  account/user/<username or team>/api
$oauth_params = array(
    'oauth_consumer_key'      => 'aaa',
    'oauth_consumer_secret'   => 'bbb'
);

$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();

what i want to do is copying user's auth and giving the auth to issue, something like this.

$credss  = $user->getcredenditals();
$issue->setCredentials( $credss  ) ;

EDIT: yahooooooooooo!! I learned form gazaret answer what to do. Here is the working code of mine

public function createIssue()
{
    $account_name = 'companyy';    
    $repo_slug = 'issuer';

    $oauth_params = array(
        'oauth_consumer_key'      => 'key',
        'oauth_consumer_secret'   => 'secret'
    );

    $issue = new issues();
    //this was the missing peace of the puzzle . one single line
    $issue->getClient()->addListener( new OAuthListener($oauth_params) );

    $issue->create($account_name, $repo_slug, array(
        'title'     => 'konu o_authlu',
        'content'   => 'içerik metin 123123',
        'kind'      => 'proposal',
        'priority'  => 'blocker'
        ));

    return;

}

回答1:


Here's how I did it. FYI you have to use an oauth consumer key & secret from a personal account and not from the company account.

protected $bbCompany = 'companyname';

protected $oauth_params = array(
    'oauth_consumer_key' => 'key',
    'oauth_consumer_secret' => 'secret'
);

protected function bitBucketIssues()
{
    $issue = new Bitbucket\API\Repositories\Issues();
    $issue->getClient()->addListener(
        new Bitbucket\API\Http\Listener\OAuthListener($this->oauth_params)
    );      

    return $issue;
}

You can then write new methods which start by creating a new instance of $issue by calling bitBucketIssues() and then making a request, e.g.:

public function fetchBitBucketAllIssues($slug)
{

    $issue = $this->bitBucketIssues();

    $response = $issue->all($this->bbCompany, $slug);

    if ($this->checkBitBucketResponse($response)) {
        return json_decode($response->getContent());
    } else {
        return null;
    }


}

This method gets all issues from the repo "slug", then checks the response in a separate method. That method is quite rudimentary but does the job for me:

protected function checkBitBucketResponse($response)
{
    $headers = $response->getHeaders();

    if ($headers[0] == "HTTP/1.1 404 NOT FOUND") {
        return false;
    }
    elseif ($headers[0] != "HTTP/1.1 200 OK") {
        throw new InternalErrorException('Bad response received from BitBucket: ' . $response->getContent());
    }
    else {
        return true;
    }
}


来源:https://stackoverflow.com/questions/26335267/sending-issue-to-bitbucket-using-oauth-via-bitbucket-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!