2

My code is based off of this: Tutorial

Only thing I did differently is that I changed the namespace classes.

The following Is the test script that I run. superior.store is a local domain

#rest_test.php

<?php
/********************************************************************
File name: rest_test.php
Description:
A PHP test script that calls the Coupon AutoGen extension
to Magento's REST API.
The Coupon AutoGen API takes:
-- the rule ID of the &quot;Generate Coupons&quot; rule to execute
-- the number of coupon codes to generate
-- the length of each coupon code
-- the format of each coupon code
The API returns the generated coupon codes, in JSON-encoded form
 ********************************************************************/

// Replace <<...>> below with the key and secret values generated for the Coupon AutoGen Test Driver
$consumerKey = 'b343c5f6fafe5d2536a5659416760c12'; // from Admin Panel's &quot;REST - OAuth Consumers page&quot;
$consumerSecret = 'ac33d9543baf672d048652159f3630c9'; // from Admin Panel's &quot;REST - OAuth Consumers page&quot;

// Set the OAuth callback URL to this script since it contains the logic
// to execute *after* the user authorizes this script to use the Coupon AutoGen API
$callbackUrl = "http://superior.store/rest_test.php";

// Set the URLs below to match your Magento installation
$temporaryCredentialsRequestUrl = "http://superior.store/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
$adminAuthorizationUrl = 'http://superior.store/admin/oauth_authorize';
$accessTokenRequestUrl = 'http://superior.store/oauth/token';
$apiUrl = 'http://superior.store/api/rest';

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 {
        // We have the OAuth client and token. Now, let's make the API call.
        $oauthClient->setToken($_SESSION['token'], $_SESSION['secret']);

        // Set the array of params to send with the request
        $ruleId = 1; // Set to the rule ID of the Generate Coupons rule
        $couponGenerationData = array();
        $couponGenerationData['qty'] = 2; // Number of coupons codes to create
        $couponGenerationData['length'] = 7; // Length of each coupon code
        // Options for format include:
        // alphanum (for alphanumeric codes), alpha (for alphabetical codes), and num (for numeric codes)
        $couponGenerationData['format'] = "alphanum"; // Use alphanumeric for the coupon code format

        // Generate coupon codes via POST
        $resourceUrl = "$apiUrl/coupondemo/rules/{$ruleId}/codes";
        $oauthClient->fetch($resourceUrl, json_encode($couponGenerationData), OAUTH_HTTP_METHOD_POST, array(
            'Accept' => 'application/json',
            'Content-Type' => 'application/json',

        ));

        // Retrieve list of created coupons via GET
        $collectionFilters = array('limit' => $couponGenerationData['qty'], 'order' => 'coupon_id', 'dir' => 'dsc');
        $oauthClient->fetch($resourceUrl, $collectionFilters, OAUTH_HTTP_METHOD_GET, array(
            'Accept' => 'application/json',
            'Content-Type' => 'application/json',
        ));
        $coupons = json_decode($oauthClient->getLastResponse(), true);

        // Display the newly generated codes to demonstrate that the Coupon AutoGen API works
        // In reality, you might put these codes in emails to customers, store them in a database, etc.
        echo "New coupon codes:<br/>";
        foreach ($coupons as $coupon) {
            echo " --> " . $coupon['code'] . "<br/>";
        }
    }
} catch (OAuthException $e) {
    print_r($e->getMessage());
    echo "<br/>";
    print_r($e->lastResponse);
}

When I run the script I get the following page

enter image description here

From the research ive done it appears that perhaps my html document maybe incorrect ? I see that its saying others available, but like type application/x-httpd-php but I tried changing the Accept and Content Type parameter and the second time I didn't get the issue but instead got this error response

enter image description here

I honestly don't know what to do next ? How do I get magento to accept json content type or what am I doing wrong ?

Thanks in advance

numerical25
  • 10,524
  • 36
  • 130
  • 209
  • http://stackoverflow.com/questions/14251851/what-is-406-not-acceptable-response-in-http – Shivam Feb 28 '14 at 05:43
  • I already read that and I thought what I did in my post was that. I changed the calls `Accept` and `Content-Type` to `application/x-httpd-php` . It got rid of the error message but the calls returned as false with a error message in the debug info like in the screenshot above saying `Invalid webservice adapter specified.`. And it goes against the tutorial. Is the tutorial incorrect ? – numerical25 Feb 28 '14 at 11:11

0 Answers0