woocommerce / wc-api-php

WooCommerce REST API PHP Library
https://packagist.org/packages/automattic/woocommerce
MIT License
520 stars 159 forks source link

Always getting a woocommerce_rest_authentication_error #37

Closed E-Mud closed 8 years ago

E-Mud commented 8 years ago

Hello.

I've been trying to use this wrapper to connect to a wordpress + woocommerce page but I always get an authentication error. I followed the steps described here to generate the key and secret for the application.

PHP: v7.0.9 Laravel Framework: v5.2.41 WC-API-PHP: v1.1.1

Code:

try {
  $woocommerce = new Client(
    'http://localhost:8000',
    'ck_792b5ebdea08a6edb6d3c415ac4f8e0bc7221553',
    'cs_f56b03cbe7536cb4c0215f476d5a4e119dbc78ab',
    [
      'wp_api' => true,
      'version' => 'wc/v1',
      'verify_ssl' => false,
      'timeout' => 120,
    ]
  );
  print_r($woocommerce->get('products'));
} catch ( HttpClientException $e ) {
  print_r($e->getMessage().PHP_EOL); // Error message.
  $lastRequest = $woocommerce->http->getRequest();
  print_r($lastRequest->getUrl().PHP_EOL); // Requested URL (string).
  print_r($lastRequest->getParameters()); // Request parameters (array).
  print_r($lastRequest->getHeaders()); // Request headers (array).
}

Output:

Error: Invalid Signature - provided signature does not match. [woocommerce_rest_authentication_error]
http://localhost:8000/wp-json/wc/v1/products?oauth_consumer_key=ck_792b5ebdea08a6edb6d3c415ac4f8e0bc7221553&oauth_nonce=64b9aeeedfdb37b08085a02c0fe549273def9ec5&oauth_signature=Xy2bUyBjEYXr10n7HbK2eQptg%2FwxRrxnwgyWETxIN5o%3D&oauth_signature_method=HMAC-SHA256&oauth_timestamp=1470306720
Array
(
    [oauth_consumer_key] => ck_792b5ebdea08a6edb6d3c415ac4f8e0bc7221553
    [oauth_nonce] => 64b9aeeedfdb37b08085a02c0fe549273def9ec5
    [oauth_signature] => Xy2bUyBjEYXr10n7HbK2eQptg/wxRrxnwgyWETxIN5o=
    [oauth_signature_method] => HMAC-SHA256
    [oauth_timestamp] => 1470306720
)
Array
(
    [Accept] => application/json
    [Content-Type] => application/json
    [User-Agent] => WooCommerce API Client-PHP/1.1.1
)

Wordpress System:

`
### WordPress Environment ###

Home URL: http://localhost:8000
Site URL: http://localhost:8000
WC Version: 2.6.2
Log Directory Writable: ✔
WP Version: 4.5.3
WP Multisite: –
WP Memory Limit: 256 MB
WP Debug Mode: –
WP Cron: ✔
Language: es_ES

### Server Environment ###

Server Info: Apache/2.4.10 (Debian)
PHP Version: 5.6.24
PHP Post Max Size: 8 MB
PHP Time Limit: 30
PHP Max Input Vars: 1000
cURL Version: 7.38.0
OpenSSL/1.0.1t

SUHOSIN Installed: –
Max Upload Size: 2 MB
Default Timezone is UTC: ✔
fsockopen/cURL: ✔
SoapClient: ❌ Tu servidor no tiene la clase SoapClient habilitada - puede que algunos plugins de puerta de enlace que utilizan SOAP no funcionen como se espera.
DOMDocument: ✔
GZip: ✔
Multibyte String: ✔
Publicar remoto: ✔
Obtén remoto: ✔

### Database ###

WC Database Version: 2.6.2
: 
woocommerce_sessions: ✔
woocommerce_api_keys: ✔
woocommerce_attribute_taxonomies: ✔
woocommerce_downloadable_product_permissions: ✔
woocommerce_order_items: ✔
woocommerce_order_itemmeta: ✔
woocommerce_tax_rates: ✔
woocommerce_tax_rate_locations: ✔
woocommerce_shipping_zones: ✔
woocommerce_shipping_zone_locations: ✔
woocommerce_shipping_zone_methods: ✔
woocommerce_payment_tokens: ✔
woocommerce_payment_tokenmeta: ✔
MaxMind GeoIP Database: ✔

### Active Plugins (22) ###

Akismet: por Automattic – 3.1.11
Asesor de Cookies: por Carlos Doral Pérez – 0.21
Contact Form 7: por Takayuki Miyoshi – 4.4.2
Content Manager: por Pixel Industry Ltd. – 1.1.9
Elvyre Core plugin: por Pixel Industry – 1.5
Google Analytics by MonsterInsights: por MonsterInsights – 5.5.2
Hello Dolly: por Matt Mullenweg – 1.6
Instagram Feed: por Smash Balloon – 1.4.6.2
Newsletter: por Stefano Lissa & The Newsletter Team – 4.5.6
Recent posts Widget: por Pixel Industry – 1.2
Regenerate Thumbnails: por Alex Mills (Viper007Bond) – 2.2.6
Revolution Slider: por ThemePunch – 4.6.93
Simple Share Buttons Adder: por Simple Share Buttons – 6.2.2
TweetScroll Widget: por Pixel Industry – 1.3.7
Social Media and Share Icons (Ultimate Social Media): por UltimatelySocial – 1.5.2
WooCommerce Customizer: por SkyVerge – 2.3.1
WooCommerce Pay for Payment: por Jörn Lund – 1.3.8
WooCommerce Redsys payment gateway: por Jesús Ángel del Pozo Domínguez – 1.0.11
WooCommerce: por WooThemes – 2.6.2
Importador de WordPress: por wordpresspuntoorg – 0.6.1
YITH Newsletter Popup: por Your Inspiration Themes – 1.0.2
YITH WooCommerce Wishlist: por YITHEMES – 2.0.16

### Settings ###

Force SSL: –
Currency: EUR (€)
Currency Position: right
Thousand Separator: .
Decimal Separator: ,
Number of Decimals: 2

### API ###

API Enabled: ✔

### WC Pages ###

Base de la tienda: #936 - /tienda/
Carrito: #937 - /carro/
Finalizar compra: #938 - /finalizar-comprar/
Mi cuenta: #939 - /mi-cuenta/

### Taxonomies ###

Product Types: external (external)
grouped (grouped)
simple (simple)
variable (variable)

### Theme ###

Name: Elvyre - Retina Ready Wordpress Theme
Version: 1.5
Author URL: http://pixel-industry.com
Child Theme: ❌ – Si estás modificando WooCommerce en un tema padre que no has creado personalmente
te recomendamos utilizar un tema hijo. Ver: : Cómo crear un tema hijo

WooCommerce Support: ✔

### Templates ###

Overrides: elvyre/woocommerce/archive-product.php
elvyre/woocommerce/content-product.php versión1.6.4 no está actualizado. La versión del sistema es2.6.1
elvyre/woocommerce/notices/error.php
elvyre/woocommerce/notices/notice.php
elvyre/woocommerce/notices/success.php
elvyre/woocommerce/single-product/product-image.php
elvyre/woocommerce/single-product.php

: Aprende cómo actualizar plantillas obsoletas
`

What am I doing wrong or what did I miss? Thanks in advance

mikejolley commented 8 years ago

Don't post in multiple places... https://github.com/woothemes/wc-api-php/issues

E-Mud commented 8 years ago

Sorry I don't understand. This is the only place I posted this.

E-Mud commented 8 years ago

Edited: Forgot to add the Laravel version

E-Mud commented 8 years ago

I just discovered that if I do it with curl I get a different error:

curl http://localhost:8000/wp-json/wc/v1/products -u ck_792b5ebdea08a6edb6d3c415ac4f8e0bc7221553:cs_f56b03cbe7536cb4c0215f476d5a4e119dbc78ab
{"code":"woocommerce_rest_cannot_view","message":"Lo siento, no puedes listar los recursos.","data":{"status":401}}
E-Mud commented 8 years ago

I solved the issue. It was related to my local environment.

For the record: Actually I wasn't using a direct connection in the same machine, but links between docker containers. That means that the URL that my app was using and the URL of the wordpress system didn't match. The discovery of the host was obviously succesful. Problem is I forgot that the URL was used for encoding the signature so if they didn't match the signature wouldn't match.

So if you don't mind I'll close this issue.