1

我已阅读此相关问题; 从 BitBucket 请求 OAuth 令牌

在上面的那个问题中,它使用 curl 。但是必须有一种方法可以使用gentero-api,因为它包含关于oauth 的php 类。

    $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但是。我找不到任何好的资源。

编辑:

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

我想要做的是复制用户的身份验证并授予身份验证,就像这样。

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

编辑:哇哇哇哇!!我从 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;

}
4

1 回答 1

0

这就是我的做法。仅供参考,您必须使用来自个人帐户而不是来自公司帐户的 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() 创建一个新的 $issue 实例,然后发出请求,例如:

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


}

此方法从存储库“slug”中获取所有问题,然后在单独的方法中检查响应。该方法非常初级,但对我有用:

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;
    }
}
于 2014-10-13T09:34:01.337 回答