简体   繁体   English

当我尝试使用PHP访问JIRA REST API时未经授权(401)

[英]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.

相关问题 当我尝试使用带有沙箱的本地主机中的 php file_get_contents() 获取 OAuth2 令牌时,Paypal Rest Api 返回 401 Unauthorized ,但在 ajax 中有效 - Paypal Rest Api returns 401 Unauthorized when i try to get OAuth2 token using php file_get_contents() in localhost with sandbox , but works in ajax 版本2中的PHP Rest API JIRA错误401 - PHP Rest API JIRA error 401 in version 2 使用php代码点击时使用gikom openfire REST API,错误显示为401未经授权 - Using gikom openfire REST API when hit using the php code , the error appears as 401 unauthorized onelogin api与php curl 401未经授权 - onelogin api with php curl 401 unauthorized 错误401,使用PHP中的curl访问REST API - Error 401 using curl in PHP to access REST API OAuth2:错误401在PHP请求中使用access_token(Discord API)未经授权 - OAuth2: Error 401 Unauthorized at PHP request with access_token (Discord API) Phonegap错误:401未经授权 - 在Phonegap中使用基本身份验证的Rest API - Phonegap Error : 401 Unauthorized - consuming Rest API with Basic Auth in Phonegap 401 未经 Adyen API 授权 - 401 Unauthorized on Adyen API Jira Oauth:401未经PHP授权oauth_problem =“ signature_invalid” - Jira Oauth: 401 Unauthorized in PHP oauth_problem=“signature_invalid” 通过 REST API (php) 在 Jira 中的受让人 - Assignee in Jira via REST API (php)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM