Magento – Invalid auth/bad request (got a 401, expected HTTP/1.1 20X or a redirect) magento rest api error

apimagento-1.9rest api

Error message below

Invalid auth/bad request (got a 401, expected HTTP/1.1 20X or a
redirect)
{"messages":{"error":[{"code":401,"message":"oauth_problem=internal_error&message=Mage
registry key \"_singleton/admin/session\" already exists"}]}}

This error showing when I try to get all products via Rest api in magento 1.9.3.4.

Below is the code

$callbackUrl = "magentohost/test.php";
$temporaryCredentialsRequestUrl = "magentohost/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
$customerAuthorizationUrl = 'magentohost/oauth/authorize';
$accessTokenRequestUrl = 'magentohost/oauth/token';
$apiUrl = 'magentohost/api/rest';
$consumerKey = 'xxxxxxxxxxxxxxxxxxxx';
$consumerSecret = 'xxxxxxxxxxxxxxxxxx';

session_start();
if (!isset($_GET['oauth_token']) && isset($_SESSION['state']) && $_SESSION['state'] == 1) {
    $_SESSION['state'] = 0;
}
try {
    $authType = ($_SESSION['state'] == 2) ? OAUTH_AUTH_TYPE_AUTHORIZATION : OAUTH_AUTH_TYPE_URI;
    $oauthClient = new OAuth($consumerKey, $consumerSecret, OAUTH_SIG_METHOD_HMACSHA1, $authType);
    $oauthClient->enableDebug();

    if (!isset($_GET['oauth_token']) && !$_SESSION['state']) {
        $requestToken = $oauthClient->getRequestToken($temporaryCredentialsRequestUrl);
        $_SESSION['secret'] = $requestToken['oauth_token_secret'];
        $_SESSION['state'] = 1;
        header('Location: ' . $customerAuthorizationUrl . '?oauth_token=' . $requestToken['oauth_token']);
        exit;
    } else if ($_SESSION['state'] == 1) {
        $oauthClient->setToken($_GET['oauth_token'], $_SESSION['secret']);
        $accessToken = $oauthClient->getAccessToken($accessTokenRequestUrl);
        $_SESSION['state'] = 2;
        $_SESSION['token'] = $accessToken['oauth_token'];
        $_SESSION['secret'] = $accessToken['oauth_token_secret'];
        header('Location: ' . $callbackUrl);
        exit;
    } else {
        $oauthClient->setToken($_SESSION['token'], $_SESSION['secret']);

        $resourceUrl = "$apiUrl/products";
        $oauthClient->fetch($resourceUrl, array(), 'GET', array('Content-Type' => 'application/json', 'Accept' => 'application/json'));
        $productsList = json_decode($oauthClient->getLastResponse());
        print_r($productsList);
    }
} catch (OAuthException $e) {
    print_r($e->getMessage());
    echo "<br/>";
    print_r($e->lastResponse);
}

Please help me to resolved this issue. Thanks in advance.

Best Answer

What role are you using to retrieve the products? Administrator, Customer or Guest? And are you running the test.php on the same host as the magento installation you are trying to reach? It seems so from your code. If so, has the host IP restriction in place? This can mean it will not allow calls from its own IP address...

In your error it says "_singleton/admin/session" already exists so at least have a look at Mage registry key "_singleton/admin/session" already exists

I don't have enough information but I think you may be trying to retrieve the products with the administrator role and your test.php is on the same host as your magento installation that you are questioning and you may have issues with IP restriction...

Related Topic