![](/img/trans.png)
[英]Gimbal API Authentication fails with 401 unauthorized (PHP cURL request)
[英]401 Unauthorized response when sending a request in cURL PHP
我正在嘗試使用 GitLab API 解決我的 gitlab 項目問題,但它似乎不起作用
<?php
$url = "https://git.exemple.com/api/v4/issues";
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_HTTPAUTH,CURLAUTH_ANY);
curl_setopt($ch,CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json',
'Authorization: PRIVATE-TOKEN xxxxxxxxxxxxxxxx'
));
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$resp = curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($e = curl_error($ch)){
echo $e;
}
else{
$decoded = json_decode($resp,true);
print_r($decoded);
}
curl_close($ch);
這是我得到的回應
Array ( [message] => 401 Unauthorized )
簡單閱讀手冊
建議您的 header 應該是:
curl_setopt($ch,CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json',
'PRIVATE-TOKEN: xxxxxxxxxxxxxxxx'
));
或者
curl_setopt($ch,CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json',
'Authorization: Bearer xxxxxxxxxxxxxxxx'
));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.