简体   繁体   中英

Unauthorized (401) when I try to access JIRA REST API with PHP

I'm trying to use JIRA REST API with PHP. When I copy the url below and paste it straight into the browser it works fine. The resulting issue is returned as json.

But with the code below it doesn't work. I get Unauthorized (401) as a return message. Yes, I have checked and double checked that the credentials are valid. This is my code:

$username = 'username';
$password = 'psw';
$url = "https://mycompany.atlassian.net/rest/api/2/issue/XXX-123";

$curl = curl_init();
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);

$result = curl_exec($curl);
echo $result;

Any ideas?

Solution: Use username and not email when providing credentials.

It turns out, even if you login with your email in JIRA, it's not the email you use here, but the username, which can be found in Jira->Settings->Profile

Try to remove https:// part from url.

Try to add

curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM