【问题标题】:sending issue to bitbucket using oauth via bitbucket api通过 bitbucket api 使用 oauth 向 bitbucket 发送问题
【发布时间】:2014-10-13 07:55:31
【问题描述】:

我已阅读此相关问题; Request OAuth token from BitBucket

在上面的那个问题中,它使用 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,但是。我找不到任何好的资源。

编辑:

//我用 basic auth 来做到这一点。 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  ) ;

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

}

【问题讨论】:

  • 见下面的答案。我发现 Generero 包装器中的文档也有些混乱,但我最终解决了。

标签: php bitbucket bitbucket-api


【解决方案1】:

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

【讨论】:

  • aaaah 现在我明白了。你用 thw issue->getclient()->addOauthListener 做到了,现在我明白了。我什至不知道问题类是否有 getclient->addlistener。谢谢,我去试试
  • 您应该使用$response-&gt;isSuccessful() 来检查响应是否成功。这样您就可以使用相同的代码来检查来自任何请求方法的响应(在 POST 上您将获得 201 而不是 200)。
猜你喜欢
  • 1970-01-01
  • 2013-10-05
  • 2020-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-21
  • 2019-12-07
  • 1970-01-01
相关资源
最近更新 更多