发送问题到位桶通过API到位桶使用OAuth(sending issue to bitbucket

2019-10-21 03:15发布

我已阅读此相关的问题; 从到位桶请求的OAuth令牌

在上面这个问题,它使用卷曲。 但必须有办法用gentlero-API来做到这一点,因为它有PHP类,它了解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'
        ));

我想要做的OAuth的那样简单,但。 我coudnt找到任何好的种源。

编辑:

//我有这样的基本身份验证做到了。 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'
));

而且有这个太; 这段代码的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();

我想要做的就是复制用户的身份验证,并给予AUTH发行,这样的事情。

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

编辑:yahooooooooooo! 我学会了形式gazaret回答做什么。 这里是我的工作代码

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;

}

Answer 1:

以下是我做到了。 FYI你必须使用个人帐户从公司帐户的OAuth用户密钥的秘密,而不是。

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;
}

然后,您可以编写通过调用bitBucketIssues(),然后发出请求,如创建$问题的一个新实例开始新的方法:

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;
    }


}

这种方法获取的所有问题从回购“鼻涕虫”,然后检查在一个单独的方法响应。 这种方法是很基本的,但做这项工作对我来说:

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;
    }
}


文章来源: sending issue to bitbucket using oauth via bitbucket api