在 PHP 中将 Curl 用于 bitbucket API

Using Curl in PHP for bitbucket APIs

我正在尝试在 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": "Client credentials missing; this request needs to be authenticated with the OAuth client id and secret", "error": "unauthorized_client"}"

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

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

但仍然没有帮助。

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

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

阅读更多here