如何解决我尝试了Magento API,但收到消息“需要401授权”
我想将Magento API用于我的网站,并且想要“全面的无头贸易”。 我运行了以下PHP代码。我得到了“令牌”。但是我无法添加客户。为什么?
401 Authorization Required nginx/1.10.1
<?php
$userData = array("username" => "API_USER","password" => "API_PATH");
$ch = curl_init("http://BASIC_AUTH_USER:BASIC_AUTH_PASSWORD@BASE_URL/rest/V1/integration/admin/token");
curl_setopt($ch,CURLOPT_CUSTOMREQUEST,"POST");
curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode($userData));
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HTTPHEADER,array("Content-Type: application/json","Content-Length: " . strlen(json_encode($userData))));
$token = curl_exec($ch);
$token= json_decode($token);
$customerData = [
'customer' => [
'email' => "user@example.com",'firstname' => "John",'lastname' => "Doe",'storeId' => 1,'websiteId' => 1
],'password' => "Demo1234"
];
$ch = curl_init("http://BASIC_AUTH_USER:BASIC_AUTH_PASSWORD@BASE_URL/rest/V1/customers");
curl_setopt($ch,json_encode($customerData));
curl_setopt($ch,"Authorization: Bearer ".$token)
);
$result = curl_exec($ch);
$result = json_decode($result);
解决方法
我在标头中添加了基本身份验证。它感动了!!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。