Skip to content

Commit f42f539

Browse files
Updated the Oauth Tips, closes woocommerce#7
1 parent e17f144 commit f42f539

File tree

1 file changed

+29
-36
lines changed

1 file changed

+29
-36
lines changed

source/includes/v2/_introduction.md

+29-36
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
# Introduction
1+
# Introduction #
22

33
Introduced in WooCommerce 2.1, the REST API allows store data to be created, read, updated, and deleted using the JSON format.
44

5-
## Requirements
5+
## Requirements ##
66

77
You must be using WooCommerce 2.1 or newer and the REST API must be enabled under `WooCommerce > Settings`. You must enable pretty permalinks, as default permalinks will not work.
88

99
<aside class="notice">
1010
Many endpoints were improving with new versions of WooCommerce, so we always recommend keeping your WooCommerce updated to work properly with this documentation.
1111
</aside>
1212

13-
## Schema
13+
## Schema ##
1414

1515
The API is accessible via this endpoint:
1616

1717
`https://www.your-store.com/wc-api/v2`
1818

1919
You may access the API over either HTTP or HTTPS. HTTPS is recommended where possible, as authentication is simpler. The API index will declare if the site supports SSL or not.
2020

21-
## Version
21+
## Version ##
2222

2323
The current API version is `v2` which takes a first-order position in endpoints. The `v1` endpoint is available in WooCommerce 2.1 / 2.2 / 2.3, but it will be removed in a future version.
2424

25-
### Differences between v1 and v2 versions
25+
### Differences between v1 and v2 versions ###
2626

2727
* v1 supports XML response format, v2 only supports JSON.
2828
* v1 does not support creating or updating (with the exception of order status) any resources, v2 supports full create/read/update/delete for all endpoints.
@@ -33,7 +33,7 @@ The current API version is `v2` which takes a first-order position in endpoints.
3333
* v1 does not include any endpoints for getting valid order statuses, v2 includes an endpoint for listing valid order statuses (`GET /orders/statuses`).
3434
* v2 supports the core features added in WooCommerce 2.2, primarily order refunds (via the `/orders/refunds` endpoint) and Webhooks (via the `/webhooks`).
3535

36-
## Requests/Responses
36+
## Requests/Responses ##
3737

3838
The default response format is JSON. Requests with a message-body use plain JSON to set or update resource attributes. Successful requests will return a `200 OK` HTTP status.
3939

@@ -49,11 +49,11 @@ Some general information about responses:
4949

5050
* Blank fields are generally included as `null` instead of being blank strings or omitted.
5151

52-
## Authentication
52+
## Authentication ##
5353

5454
There are two aways to authenticate with the API, depending on whether the site supports SSL or not. Remember that the Index endpoint will indicate if the site supports SSL or not.
5555

56-
### Over HTTPS
56+
### Over HTTPS ###
5757

5858
You may use [HTTP Basic Auth](http://en.wikipedia.org/wiki/Basic_access_authentication) by providing the API Consumer Key as the username and the API Consumer Secret as the password.
5959

@@ -72,10 +72,11 @@ Occasionally some servers may not properly parse the Authorization header (if yo
7272
curl https://www.example.com/wc-api/v2/orders?consumer_key=123&consumer_secret=abc
7373
```
7474

75-
### Over HTTP
75+
### Over HTTP ###
76+
7677
You must use [OAuth 1.0a "one-legged" authentication](http://tools.ietf.org/html/rfc5849) to ensure API credentials cannot be intercepted. Typically you may use any standard OAuth 1.0a library in your language of choice to handle the authentication, or generate the necessary parameters by following these instructions.
7778

78-
#### Generating an OAuth signature
79+
#### Generating an OAuth signature ####
7980

8081
1) Set the HTTP method for the request:
8182

@@ -109,33 +110,25 @@ when encoded:
109110

110111
If you are having trouble generating a correct signature, you'll want to review your string to sign for errors with encoding. The [authentication source](https://github.com/woothemes/woocommerce/blob/master/includes/api/class-wc-api-authentication.php#L177) can also be helpful in understanding how to properly generate the signature.
111112

112-
#### OAuth Tips
113+
#### OAuth Tips ####
113114

114115
* The OAuth parameters must be added as query string parameters and *not* included in the Authorization header. This is because there is no reliable cross-platform way to get the raw request headers in WordPress.
115-
116116
* The require parameters are: `oauth_consumer_key`, `oauth_timestamp`, `oauth_nonce`, `oauth_signature`, and `oauth_signature_method`. `oauth_version` is not required and must be omitted.
117-
118117
* HMAC-SHA1 or HMAC-SHA256 are the only accepted hash algorithms.
119-
120118
* The OAuth nonce can be any randomly generated 32 character (recommended) string that is unique to the consumer key. Read more suggestions on [generating a nonce](https://dev.twitter.com/discussions/12445) on the Twitter API forums.
121-
122119
* The OAuth timestamp should be the unix timestamp at the time of the request. The API will deny any requests that include a timestamp that is outside of a 15 minute window to prevent replay attacks.
123-
124120
* You must use the store URL provided by the index when forming the base string used for the signature, as this is what the server will use. (e.g. if the store URL includes a `www` sub-domain, you should use it for requests)
125-
126121
* Some OAuth libraries add an ampersand to the provided secret key before generating the signature. This does not adhere to the OAuth spec and the ampersand should be removed prior to generating the signature.
127-
128122
* You may test your generated signature using LinkedIn's [OAuth test console](http://developer.linkedinlabs.com/oauth-test/) -- leave the member token/secret blank.
129-
130123
* Twitter has great instructions on [generating a signature](https://dev.twitter.com/docs/auth/creating-signature) with OAuth 1.0a, but remember tokens are not used with this implementation.
131-
132124
* Note that the request body is *not* signed as per the OAuth spec, see [Google's OAuth 1.0 extension](https://oauth.googlecode.com/svn/spec/ext/body_hash/1.0/oauth-bodyhash.html) for details on why.
125+
* If including filter fields in your request, it saves a lot of trouble if you can order your filter fields alphabetically before submitting. Many Oauth libraries won't order subquery fields properly, resulting in invalid signatures.
133126

134-
## Parameters
127+
## Parameters ##
135128

136129
All endpoints accept optional parameters which can be passed as an HTTP query string parameter, e.g. `GET /orders?status=completed`. There are common parameters and endpoint-specific parameters which are documented along with that endpoint.
137130

138-
### Filter Parameter
131+
### Filter Parameter ###
139132

140133
All endpoints accept a `filter` parameter that scopes individual filters using brackets, like date filtering:
141134

@@ -147,7 +140,7 @@ Multiple `filter` parameters can be included and intermixed with other parameter
147140

148141
Note that the following filters are supported for all endpoints except the `reports` endpoint, which has it's own set of filters that are documented along with that endpoint.
149142

150-
#### Available Filters
143+
#### Available Filters ####
151144

152145
| Filter | Description |
153146
|--------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
@@ -165,7 +158,7 @@ Note that the following filters are supported for all endpoints except the `repo
165158

166159
Note that Dates should be provided in [RFC3339](http://www.ietf.org/rfc/rfc3339.txt) format in UTC timezone: `YYYY-MM-DDTHH:MM:SSZ`. You may omit the time and timezone if desired.
167160

168-
### Fields Parameter
161+
### Fields Parameter ###
169162

170163
You may limit the fields returned in the response using the `fields` parameter:
171164

@@ -183,7 +176,7 @@ Sub-fields can't be limited for resources that have multiple structs, like an or
183176

184177
`GET /orders?fields=line_items.product_id`
185178

186-
## Pagination
179+
## Pagination ##
187180

188181
Requests that return multiple items will be paginated to 10 items by default. This default can be changed by the site administrator by changing the `posts_per_page` option. Alternatively the items per page can be specifed with the `?filter[limit]` parameter:
189182

@@ -201,7 +194,7 @@ Page number is 1-based and ommiting the `?page` parameter will return the first
201194

202195
The total number of resources and pages are always included in the `X-WC-Total` and `X-WC-TotalPages` HTTP headers.
203196

204-
## Link Header
197+
## Link Header ##
205198

206199
Pagination info is included in the [Link Header](http://tools.ietf.org/html/rfc5988). It's recommended that you follow these values instead of building your own URLs where possible.
207200

@@ -221,7 +214,7 @@ The possible `rel` values are:
221214
| `first` | Shows the URL of the first page of results |
222215
| `prev` | Shows the URL of the immediate previous page of results |
223216

224-
## Errors
217+
## Errors ##
225218

226219
Occasionally you might encounter errors when accessing the API. There are four possible types:
227220

@@ -284,7 +277,7 @@ Occasionally you might encounter errors when accessing the API. There are four p
284277

285278
Errors return both an appropriate HTTP status code and response object which contains a `code` and `message` attribute. If an endpoint has any custom errors, they are documented with that endpoint.
286279

287-
## HTTP Verbs
280+
## HTTP Verbs ##
288281

289282
The API uses the appropriate HTTP verb for each action:
290283

@@ -296,7 +289,7 @@ The API uses the appropriate HTTP verb for each action:
296289
| `POST` | Used for creating resources |
297290
| `DELETE` | Used for deleting resources |
298291

299-
## JSONP Support
292+
## JSONP Support ##
300293

301294
The API supports JSONP by default. JSONP responses uses the `application/javascript` content-type. You can specify the callback using the `?_jsonp` parameter for `GET` requests to have the response wrapped in a JSON function:
302295

@@ -345,7 +338,7 @@ curl https://example.com/wc-api/v2/orders/count?_jsonp=ordersCount \
345338
}
346339
```
347340

348-
## Webhooks
341+
## Webhooks ##
349342

350343
Webhooks are an experimental feature in the v2 REST API. They must be managed using the REST API endpoints as a UI is not yet available. The `WC_Webhook` class manages all data storage/retrieval from the custom post type, as well as enqueuing a webhook's actions and processing/delivering/logging the webhook. On `woocommerce_init`, active webhooks are loaded and their associated hooks are added.
351344

@@ -357,7 +350,7 @@ Each webhook has:
357350
* secret: an optional secret key that is used to generate a HMAC-SHA256 hash of the request body so the receiver can verify authenticity of the webhook
358351
* hooks: an array of hook names that are added and bound to the webhook for processing
359352

360-
### Topics
353+
### Topics ###
361354

362355
The topic is a combination resource (e.g. order) and event (e.g. created) and maps to one or more hook names (e.g. `woocommerce_checkout_order_processed`). Webhooks can be created using the topic name and the appropriate hooks are automatically added.
363356

@@ -370,7 +363,7 @@ Core topics are:
370363

371364
Custom topics can also be used which map to a single hook name, so for example you could add a webhook with topic `action.woocommerce_add_to_cart` that is triggered on that event. Custom topics pass the first hook argument to the payload, so in this example the `cart_item_key` would be included in the payload.
372365

373-
### Delivery/Payload
366+
### Delivery/Payload ###
374367

375368
Delivery is done using `wp_remote_post()` (HTTP POST) and processed in the background by default using wp-cron. A few custom headers are added to the request to help the receiver process the webhook:
376369

@@ -383,7 +376,7 @@ Delivery is done using `wp_remote_post()` (HTTP POST) and processed in the backg
383376

384377
The payload is JSON encoded and for API resources (coupons,customers,orders,products), the response is exactly the same as if requested via the REST API.
385378

386-
### Logging
379+
### Logging ###
387380

388381
Requests/responses are logged as comments on the webhook custom post type. Each delivery log includes:
389382

@@ -397,15 +390,15 @@ After 5 consecutive failed deliveries (as defined by a non HTTP 2xx response cod
397390

398391
Delivery logs can be fetched through the REST API endpoint or in code using `WC_Webhook::get_delivery_logs()`
399392

400-
### Endpoints
393+
### Endpoints ###
401394

402395
See the webhook resource section.
403396

404-
## Troubleshooting
397+
## Troubleshooting ##
405398

406399
* Nginx - Older configurations of Nginx can cause issues with the API, see [this issue](https://github.com/woothemes/woocommerce/issues/5616#issuecomment-47338737) for details
407400

408-
## Tools
401+
## Tools ##
409402

410403
* [WooCommerce REST API Client Library](https://github.com/kloon/WooCommerce-REST-API-Client-Library) - A simple PHP client library by Gerhard Potgieter.
411404
* [WooCommerce API Client](https://github.com/mac2000/woo-commerce-api-client) - Guzzle wrapper around WooCommerce v2 API plus missign api helpers.

0 commit comments

Comments
 (0)