[英]Unauthorized (401) when I try to access JIRA REST API with PHP
I'm trying to use JIRA REST API with PHP. 我正在尝试使用PHP的JIRA REST API。 When I copy the url below and paste it straight into the browser it works fine.
当我复制下面的网址并将其直接粘贴到浏览器中时,它可以正常工作。 The resulting issue is returned as json.
生成的问题以json的形式返回。
But with the code below it doesn't work. 但是使用下面的代码它不起作用。 I get Unauthorized (401) as a return message.
我收到Unauthorized(401)作为回复消息。 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 事实证明,即使您在JIRA中使用您的电子邮件登录,也不是您在此处使用的电子邮件,而是用户名,可以在Jira-> Settings-> Profile中找到
Try to remove https://
part from url. 尝试从网址中删除
https://
部分。
Try to add 尝试添加
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.