【问题标题】:I tried Magento API, but I got message "401 Authorization Required"我尝试了 Magento API,但收到消息“需要 401 授权”
【发布时间】:2020-08-12 04:56:47
【问题描述】:

我想为我的网站使用 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, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($customerData));
curl_setopt($ch, CURLOPT_HTTPHEADER,
    array("Content-Type: application/json", "Authorization: Bearer ".$token)
);

$result = curl_exec($ch);
$result = json_decode($result);

【问题讨论】:

标签: rest magento magento2


【解决方案1】:

我在标头中添加了基本身份验证。动了!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-04
    相关资源
    最近更新 更多