Magento – how to access token of REST Api in magento 1.9.3.1

magento-1.9token

Hi i searched the around google about this, but nothing find.
can anyone help me to access the token from rest api in magento.

I tried this code in a php file on root and getting blank screen(no output).

<?php
/**
 * Example of retrieving the products list using Admin account via Magento REST API. OAuth authorization is used
 * Preconditions:
 * 1. Install php oauth extension
 * 2. If you were authorized as a Customer before this step, clear browser cookies for 'yourhost'
 * 3. Create at least one product in Magento
 * 4. Configure resource permissions for Admin REST user for retrieving all product data for Admin
 * 5. Create a Consumer
 */
// $callbackUrl is a path to your file with OAuth authentication example for the Admin user
$callbackUrl = "http://192.168.0.78/pooja/testmagento/";
$temporaryCredentialsRequestUrl = "http://192.168.0.78/pooja/testmagento/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
$adminAuthorizationUrl = 'http://192.168.0.78/pooja/testmagento/admin/oAuth_authorize';
$accessTokenRequestUrl = 'http://192.168.0.78/pooja/testmagento/oauth/token';
$apiUrl = 'http://192.168.0.78/pooja/testmagento/api/rest';
$consumerKey = '5c795558a40557839702605ddf3f6a90';
$consumerSecret = '56a0e944528b995baab7bab4b1a9c53b';

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);
        echo $_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 {
        echo "a= ".$oauthClient->setToken($_SESSION['token'], $_SESSION['secret']);

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

Thanks.
Pooja

Best Answer

You cannot find the token and token secret from Magento backend. You need to query these from the database directly.

First login to Magento backend and go to System->Web Services->REST - Oauth Consumers. Take a note of the oauth consumer you need the keys for. You can also find consumer_key and consumer_secret from there if you click on that consumer's row. However it's easy to find the keys from DB also:

The consumers and secrets are stored in DB table oauth_consumer. Find the necessary user by column "name" from there and copy the columns "key" and "secret". These are "consumer_key" and "consumer_secret". Take a note of entity_id in oauth_consumer table.

Then look at DB table oauth_token. Find a record by consumer_id and copy columns "token" and "secret".

Now you have all the 4 keys that are necessary to make a fully authenticated Oauth request towards this Magento instance.

Reference: This is a duplicate - Rest api, tokens?

Related Topic