• Resolved JCLondon

    (@jclondon)


    Hi,

    I have a WooCommerce website and I am building an app for it (HTML, ANGULARJS). I need to get the products from the website to show them into the app and I am using the REST API.

    To get the products from WooCommerce I am using the WooCommerce REST API PHP Client Library and I manage to get the products appear on screen via echo jsonencode.

    I need to get the products via the URL but when I go to http://example.com/wc-api/v3/products I get the error

    {"errors":[{"code":"woocommerce_api_authentication_error","message":"oauth_consumer_key parameter is missing"}]}

    What have I missed? How do I obtain the oauth consumer key and where do I add it?

    Thanks
    Jeff

    https://wordpress.org/plugins/woocommerce/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Contributor Claudio Sanches

    (@claudiosanches)

    Just follow the docs: https://packagist.org/packages/automattic/woocommerce

    Example:

    <?php
    require __DIR__ . '/vendor/autoload.php';
    
    use Automattic\WooCommerce\Client;
    use Automattic\WooCommerce\HttpClient\HttpClientException;
    
    $url            = 'http://example.com';
    $consumerKey    = 'ck_xxxxxxxxxxxx'; // Change for your consumer key
    $consumerSecret = 'cs_xxxxxxxxxxxx'; // Change for your consumer secret
    $options        = [];
    
    $api = new Client($url, $consumerKey, $consumerSecret, $options);
    
    try {
    	$results = $woocommerce->get('products');
    	return json_encode($results);
    } catch (HttpClientException $e) {
    	return json_encode(['error' => ['code'=> [$e->getResponse()->getCode()], 'message'=> $e->getMessage()]]);
    }
    
    Thread Starter JCLondon

    (@jclondon)

    Hi Claudio, thank you so much for your reply, I have started from scratch again and followed the docs for what I can understand from it.

    I have successfully installed the library via composer and I have created a file products.php in which I have added your code with the relative correct URL, key and secret.

    I have uploaded all the files in the root on the server and when I go to http://example.com/products.php I get a fatal error:

    Fatal error: Call to a member function get() on a non-object in /home/wardrob0/public_html/products.php on line 15

    on line 15 in products.php I have

    $results = $woocommerce->get('products');

    Is there anything wrong with that line? I can’t figure it out from the documentation.

    Thank you

    Thread Starter JCLondon

    (@jclondon)

    Or should I put the files and folders somewhere else?

    Thank you

    Thread Starter JCLondon

    (@jclondon)

    I have solved it, sorry. It was my bad I didn’t notice the different variables.

    In case someone else needs this, the correct code is this

    <?php
    require __DIR__ . '/vendor/autoload.php';
    
    use Automattic\WooCommerce\Client;
    use Automattic\WooCommerce\HttpClient\HttpClientException;
    
    $url            = 'http://example.com';
    $consumerKey    = 'ck_xxxxxxxxxxxx'; // Change for your consumer key
    $consumerSecret = 'cs_xxxxxxxxxxxx'; // Change for your consumer secret
    $options        = [];
    
    $woocommerce = new Client($url, $consumerKey, $consumerSecret, $options);
    
    try {
    	$results = $woocommerce->get('products');
    	return json_encode($results);
    } catch (HttpClientException $e) {
    	return json_encode(['error' => ['code'=> [$e->getResponse()->getCode()], 'message'=> $e->getMessage()]]);
    }
    Thread Starter JCLondon

    (@jclondon)

    I have followed the docs but I do still get the same error.

    I have uploaded all the files in the root on the server, is this the correct place where I should put the files?

    root
    – vendor
    – products.php (in this file I have added the example code you have sent me)

    I need to get the products via the URL but when I go to http://example.com/wc-api/v3/products I get the error

    {“errors”:[{“code”:”woocommerce_api_authentication_error”,”message”:”oauth_consumer_key parameter is missing”}]}

    What am I doing wrong? Do I need to place the files somewhere else? Do I need to add something to the file products.php?

    Many thanks
    Jeff

    Hey,

    Did you solve it?? Let me know also even i am confused where to upload the library files and setup.

    Thanks,

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Authentication error with REST API’ is closed to new replies.