【问题标题】:I get an authentication error with a json post我收到一个 json 帖子的身份验证错误
【发布时间】:2020-05-23 09:55:23
【问题描述】:

我正在尝试验证,我的请求结果返回错误。

请求模型

方法:发布,端点:/api/authenticate,标头变量: [{"key":"Content-Type","value":"application/json","enabled":true}], 正文参数:用户名:字符串,密码:字符串, 身份验证类型:字符串

样品申请

POST /api/authenticate 
Host: mpop-sit.hepsiburada.com
Content-Type: application/json
{
   "username": "xyz_dev",
   "password": "XYZ_dev123!",
   "authenticationType": "INTEGRATOR"
}

我发送的请求

$url = 'https://mpop-sit.hepsiburada.com//api/authenticate';
$ch = curl_init($url);
$header = array(
    'Content-Type: application/json',
    'Authorization: Bearer '. base64_encode('xyz_dev:XYZ_dev123!:INTEGRATOR'),
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
$return=json_decode($result,true);
print_r($return);

这是返回的查询结果和我收到的错误。 你认为我可能在哪里犯了错误?

数组([时间戳] => 2020-02-07T09:01:47.426+0000 [状态] => 500 [错误] => 内部服务器错误 [异常] => io.jsonwebtoken.MalformedJwtException [message] => JWT 字符串必须 正好包含 2 个句点字符。找到:0 [路径] => //api/认证)

【问题讨论】:

  • 不,我检查了但我没有得到任何结果
  • 将网址更改为https://mpop-sit.hepsiburada.com/api/authenticatehttps://mpop-sit.hepsiburada.com/api/authenticate/
  • 谢谢,但我得到了同样的错误:(
  • @Mahlika 将此添加到代码末尾echo curl_error($ch); curl_close($ch); 第一个会给您正确的错误,第二个将关闭 curl 连接。我在上面的评论中使用您的代码和第二个网址在本地主机上收到 SSL 证书问题:无法获取本地颁发者证书。这是正确的网址:https://mpop-sit.hepsiburada.com/api/authenticate/ 我认为,请参阅此帖子以获取 ssl stackoverflow.com/a/59919558/12232340

标签: php json jwt


【解决方案1】:

您需要此端点的身份验证标头吗?

因为示例请求希望您像这样在请求正文中发送参数:

$post = [
   'username' => 'xyz_dev',
   'password' => 'XYZ_dev123!',
   'authenticationType' => 'INTEGRATOR'
];

$url = 'https://mpop-sit.hepsiburada.com//api/authenticate';
$ch = curl_init($url);
$header = array('Content-Type: application/json');
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
$return=json_decode($result,true);
print_r($return);

所以不需要 Authentication: Bearer ... 标头,它也会以不同的方式创建。

【讨论】:

  • 声明中,认证应该是bearer,而不是basic!说有趣
  • 通常,bearer 使用的身份验证凭据包含在 HTTP 标头中,而不是正文中。
  • @MikeRobinson 是的,但是 Mahlika 给出的示例请求看起来根本不需要身份验证标头。无论如何,用户名、密码和 authencationType 的组合不会构成 JWT 令牌(请参阅JWT-Token
【解决方案2】:

这应该可以正常工作!我收到拒绝访问错误,因此,这意味着代码可以正常工作。

请注意,您可能需要将 http_build_query($fields) 更改为 $fields

$url = "https://mpop-sit.hepsiburada.com/api/authenticate/";
$token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; 
$fields = array(
   "username" => "xyz_dev",
   "password" => "XYZ_dev123!"
);

$header = array(
    'Content-Type: application/json',
    'Authorization' => 'Bearer ' . $token,
);

//open curl connection
$ch = curl_init();

//set the url, fields, vars
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($fields));
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false); // SSL false if not required
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false); //False if not required
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//execute fields
$result = curl_exec($ch);
//return result echo $result; if you need
echo curl_error($ch);
//close curl connection
curl_close($ch);

如果有效,请告诉我!

更新:如果您想使用 ssl,它可以让您免遭黑客攻击。

按照此答案中的步骤激活 ssl:https://stackoverflow.com/a/59919558/12232340

【讨论】:

  • 请不要关闭 SSL 验证。它使您的代码易受攻击。
【解决方案3】:

这里有两个斜线,所以它不起作用:

$url = 'https://mpop-sit.hepsiburada.com//api/authenticate';

【讨论】:

    猜你喜欢
    • 2020-08-27
    • 1970-01-01
    • 2014-06-08
    • 2021-08-19
    • 1970-01-01
    • 2019-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多