I am new in Magento. I want to use Magento Rest API for Android. I got oAuth_token and oauth_verifier.After passing oAuth_token and oauth_verifier,I got an error.Invalid auth/bad request (got a 400, expected HTTP/1.1 20X or a redirect)
<br/>oauth_problem=parameter_absent&oauth_parameters_absent=oauth_verifier.
Here is my code:
<?php
ini_set('display_errors', '1');
$callbackUrl_1 = "http://demo.com/magento.com/admin123.php";
$callbackUrl = "http://demo.com/magento.com/sendToken.php";
$temporaryCredentialsRequestUrl = "http://demo.com/magento.com/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
//$adminAuthorizationUrl = 'http://demo.com/magento.com/admin123/oAuth_authorize';
$adminAuthorizationUrl = 'http://demo.com/magento.com/oauth/authorize';
$accessTokenRequestUrl = 'http://demo.com/magento.com/oauth/token';
$apiUrl = 'http://demo.com/magento.com/api/rest';
$consumerKey = '88a6142021c1cdfed92b0954a94fc066';
$consumerSecret = 'bedc0ede692fe06d4b12821bb21f7c3b';
session_start();
//echo "SESSION state".$_SESSION['state'];
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;
//echo "oauth_token ".$requestToken['oauth_token']."<br>";
//echo "oauth_token_secret ".$requestToken['oauth_token_secret'];die;
header('Location: ' . $adminAuthorizationUrl . '?oauth_token=' . $requestToken['oauth_token']."&oauth_token_secret=".$requestToken['oauth_token_secret']);
exit;
} else if ($_SESSION['state'] == 1) {
// echo "oauth_token ".$_GET['oauth_token'];die;
$oauthClient->setToken($_GET['oauth_token'],$_SESSION['oauth_verifier']);
$accessToken = $oauthClient->getAccessToken($accessTokenRequestUrl);
$_SESSION['state'] = 2;
$_SESSION['token'] = $accessToken['oauth_token'];
$_SESSION['secret'] = $accessToken['oauth_token_secret'];
$_SESSION['example'] = $accessToken['oauth_token'];
header('Location: ' . $callbackUrl);
exit;
} else {
$_SESSION['state'] = 0;
$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);
}
?>