【问题标题】:Using Curl in PHP for bitbucket APIs在 PHP 中为 bitbucket API 使用 Curl
【发布时间】:2018-02-13 12:08:16
【问题描述】:

我正在尝试在PHP 中学习curl,我尝试实现具有以下身份验证语法的bitbucket API:

$ curl -X POST -u "client_id:secret" \
  https://bitbucket.org/site/oauth2/access_token -d grant_type=password \
  -d username={username} -d password={password}

这是根据文档:https://confluence.atlassian.com/bitbucket/oauth-on-bitbucket-cloud-238027431.htmlPHP 中使用时,我做了这样的事情:

$postData = array(
    'grant_type' => 'password',
    'username' => '*******',
    'password' => '**********'
);
$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://bitbucket.org/site/oauth2/access_token',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => $postData,
));

$response = curl_exec($curl);

但是我遇到了一个错误

"{"error_description": "缺少客户端凭据;此请求需要使用 OAuth 客户端 ID 和密钥进行身份验证", "error": "unauthorized_client"}"

我也尝试过像这样使用 client_id 和 secret:

$postData = array(
    'grant_type' => 'password',
    'client_id' => '*******',
    'secret' => '**********'
);

但仍然没有帮助。

【问题讨论】:

    标签: php curl oauth bitbucket bitbucket-api


    【解决方案1】:

    您缺少-u 标志,该标志以base-64 编码您的"client_id:secret" 字符串并将其设置在Authorization 标头中。

    要在 PHP 中实现其效果,请设置 CURLOPT_USERPWD 选项。

    阅读更多here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-23
      • 2020-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-03
      • 2017-02-10
      • 1970-01-01
      相关资源
      最近更新 更多