I'm trying to create a product using magento REST api.
I created a OAuth Consumer in magento admin and got key and secret key, I also assigned a OAuth consumer to the user. (Followed http://help.sweettoothrewards.com/article/363-rest-api-setup )
But, I'm getting "An error occurred. Your authorization request is invalid." error.
enter image description here I think, there is something wrong with callback url because of that token is not generating but not sure.
I googled, but did not got any working solution.
I followed http://devdocs.magento.com/guides/m1x/api/rest/introduction.html but it did not worked.
<?php
$callbackUrl = "http://magentohost.com/oauth_admin.php";
$temporaryCredentialsRequestUrl = "http://magentohost.com/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
$adminAuthorizationUrl = 'http://magentohost.com/admin/oauth_authorize';
$accessTokenRequestUrl = 'http://magentohost.com/oauth/token';
$apiUrl = 'http://magentohost/api/rest';
$consumerKey = 'myconsumerkey';
$consumerSecret = 'myconsumersecret';
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: ' . $adminAuthorizationUrl . '?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' => '*/*'));
$productsList = json_decode($oauthClient->getLastResponse());
print_r($productsList);
}
} catch (OAuthException $e) {
print_r($e);
}
Please let me know what I'm doing wrong in this? Any suggestions always welcome.
Thanks
1 Answer 1
After struggling many hours on this issue, I comes to know the exact problem.
The issue was with https, my domain was forced to use https and after disabling this, its working fine.
Thanks
-
Hi Parveen, I have the same problem. Do you mean you had to make the request with HTTPS or did you edit your server so HTTPS was not required.Ankur– Ankur2017年03月31日 22:19:00 +00:00Commented Mar 31, 2017 at 22:19
-
yes, i edited server so that not require https.Parveen Kumar– Parveen Kumar2017年04月03日 11:55:42 +00:00Commented Apr 3, 2017 at 11:55