Php 通过bitbucket api使用oauth向bitbucket发送问题

Php 通过bitbucket api使用oauth向bitbucket发送问题,php,bitbucket,bitbucket-api,Php,Bitbucket,Bitbucket Api,我读过这个相关的问题; 在上面的问题中,它使用curl。但肯定有一种方法可以用GentleroAPI实现 因为它有关于oauth的php类 $bb_user = 'myuser_name'; $bb_pass = 'mypasss'; $account_name = 'account_name'; $repo_slug = 'repo_name'; $issue = new issues(); $issue->setCreden

我读过这个相关的问题;

在上面的问题中,它使用curl。但肯定有一种方法可以用GentleroAPI实现 因为它有关于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这样简单,但是。我找不到任何好的资源

编辑:

//我用basic auth这样做。

还有这个;此代码不支持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();
EDIT:yahoooooo我从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;

}

我是这样做的。仅供参考,您必须使用来自个人帐户而非公司帐户的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;
    }


}
此方法从repo“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;
    }
}

见下面的答案。我发现gentlero包装中的文档也有点混乱,但我最终还是解决了。啊,现在我明白了。你用问题->getclient()->addOauthListener解决了这个问题,现在我明白了。我甚至不知道问题类是否有一个getclient->addlistener。谢谢,我要试试。你应该使用
$response->issusccessful()
来检查响应是否成功。这样,您就可以使用相同的代码来检查来自任何请求方法的响应(在POST中,您将得到201而不是200)。
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;
    }
}