0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.