简体   繁体   中英

PHP Rest API JIRA error 401 in version 2

I am working with JIRA API's But when I called curl getting 401 error. Please let me where was my mistake.

The response is:

Unauthorized (401)

Encountered a "401 - Unauthorized" error while loading this page.

My Code is given below:

$username = '********@gmail.com';
$password = '********';

$url = 'https://vikasxtreem.atlassian.net/rest/api/2/issue/JRA-9';

$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);

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

You need to you construct and send basic auth headers yourself. To do this you need to perform the following steps:

  • Build a string of the form username:password
  • Base64 encode the string
  • Supply an "Authorization" header with content "Basic " followed by the encoded string.

Make the request as follows:

curl -D- -X GET -H "Authorization: Basic ZnJlZDpmcmVk" -H "Content-Type: application/json" "https://vikasxtreem.atlassian.net/rest/api/2/issue/JRA-9"

And, as already mentioned, at the following to your call:

curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

Add

curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

to the list of options.

Also check whether you're going through a proxy which requires authentication too.

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