I'm trying to learn curl
in PHP
, I tried implementing bitbucket
API which has following syntax for authentication:
$ curl -X POST -u "client_id:secret" \
https://bitbucket.org/site/oauth2/access_token -d grant_type=password \
-d username={username} -d password={password}
This is as per the documentation: https://confluence.atlassian.com/bitbucket/oauth-on-bitbucket-cloud-238027431.html
Which while using in PHP
I did something like this:
$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);
But I'm getting an error
"{"error_description": "Client credentials missing; this request needs to be authenticated with the OAuth client id and secret", "error": "unauthorized_client"}"
I tried using client_id and secret too like this:
$postData = array(
'grant_type' => 'password',
'client_id' => '*******',
'secret' => '**********'
);
But still no help.
You're missing the -u
flag, which base-64 encodes your "client_id:secret"
string and sets it in the Authorization
header.
To accomplish its effect in PHP, set the CURLOPT_USERPWD
option.
Read more here.