Acces to my JIRA cloud via PHP cURL and token

Enes_Sijaric January 28, 2020

I want to set one operation automatically, to download one file from my JIRA Atlassian account and save it in my server every day at a certain time. Very important is that my workspace using the Jira cloud. I want to download a file from this link:

 https://workspacename.atlassian.net/secure/admin/CloudExport.jspa

I created API TOKEN for my account and try access via PHP cURL.

$user = 'name@xxx.com';
$password = 'pass.';

 

$url = "https://workspacename.atlassian.net/rest/api/2/project";

$ch = curl_init();

 

$headers = array(

'Accept: application/json',

'Content-Type: application/json',
'Authorization: Bearer token_example'

);

 

$test = "This is the content of the custom field.";

 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_VERBOSE, 1);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");

//curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_USERPWD, "$user:$password");

 

$result = curl_exec($ch);
$ch_error = curl_error($ch);

 

if ($ch_error) {

echo "cURL Error: $ch_error";

} else {

echo $result;

}

 

curl_close($ch);

But I've got this error: {"error": "Failed to parse Connect Session Auth Token"}

Can someone help me? Did I something wrong?

 

1 answer

1 accepted

1 vote
Answer accepted
Dario B
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 29, 2020

Hello @Enes_Sijaric ,

The problem here is that you are trying to use Bearer Authentication (aka token authentication) while you are supposed to use Basic Authentication using your Atlassian Account email address and the API Token associated to it as credentials.

In other words, in your code you will have to remove the header for bearer authentication and use email address and api token as username and password.

 

For details please see:

 

Let me know if this helps.

 

Cheers,
Dario

Enes_Sijaric January 30, 2020

@Dario B thank you! It works. Now my code looks like this:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://xxx.atlassian.net/rest/api/2/project');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');

curl_setopt($ch, CURLOPT_USERPWD, 'email@xxx.com' . ':' . 'token');

$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}print_r($result);
curl_close($ch);

How I can access to this page : 

https://xxx.atlassian.net/secure/admin/CloudExport.jspa

via API? What is the URL?

Like # people like this
Dario B
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 31, 2020

Hi @Enes_Sijaric ,

You are very welcome :) 

However, if you are trying to automate Cloud backups, you may want to have a look at the (working) sample scripts provided by me and other colleagues:

In there you can find the endpoints to be called for Jira and Confluence and you can see the logic to be followed in order to automate the backup process.

However, please keep in mind that the above scripts are not officially supported since they are using internal endpoints that might change without prior notice. For details please see:

 

Finally, since my answer helped, please spare some time to click on the ACCEPT ANSWER button that's on the top of my first reply so that this thread will be marked as solved.

 

Cheers,
Dario

Suggest an answer

Log in or Sign up to answer