Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3663

Not able to use Magento REST APIs using OAuth

$
0
0

I am using Magento version 1.7.0.2 and trying to use Magento Rest APIs using OAuth Integration.I have installed OAuth and following is the snippet of code that I have put in root directory of Magento and I am running it in web browser by typing http://x.x.x.x:5009/oauth_customer.php

$callbackUrl = "http://x.x.x.x:5009/oauth_customer.php";$temporaryCredentialsRequestUrl = "http://x.x.x.x:5009/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);$adminAuthorizationUrl = 'http://x.x.x.x:5009/oauth/authorize';$accessTokenRequestUrl = "http://x.x.x.x:5009/oauth/token";$apiUrl = "http://x.x.x.x:5009/api/rest";$consumerKey = 'yourconsumerkey';$consumerSecret = 'yourconsumersecret';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);    $productsList = json_decode($oauthClient->getLastResponse());    print_r($productsList);}} catch (OAuthException $e) {   print_r($e);}

http://x.x.x.x:5009 is ip address followed 5009 where 5009 is port number specified.When we run this in the browser, I always get the following error -

Invalid auth/bad request (got a 401, expected HTTP/1.1 20X or a redirect)oauth_problem=signature_invalid&debug_sbs=Bya6oE4ujTEEFLVL6Mm04PqTA4g=

I am not able to get this work.

Note - I have generated consumer key and secret key. Not sure of how created user credentials with customer access to REST API Resources fit into the above script.

Also I want to know if we can use Magento APIs on any non Magento site with oAuth integration programatically without the user having to grant access to application each time to generate request token.


Viewing all articles
Browse latest Browse all 3663

Trending Articles