WooCommerce API for mobile App

2019-01-25 11:12发布

问题:

i am planning to develop a native mobile Android App for WooCommerce shops.

I had a look at their REST API documentation here: http://docs.woocommercev2.apiary.io/ I already started to test it but when i do different calls

GET /orders let's say it returns all the orders of the shop.

Does anyone have any idea how can i develop a enduser app using their API.

for example:

GET /products

PUT /order (create a order for the logged in User)

GET /order (get orders of the logged in User)

Any idea is appreciated :)

Thanks in advance.

回答1:

I would suggest this steps

First thing you can Enable Api for woocommerce from the backend – http://docs.woothemes.com/document/woocommerce-rest-api/

https://www.npmjs.com/package/woocommerce use this link, which has all the methods to interacting with woocommerce. Otherwise using lightweight middleware, it help to connect woocommerce server & return JSON data to your app.

Write a service using ionic framework and talk to your thin middleware client. Do not forget to cache the data (using local storage), so that you don’t hit the server all the time. - Contus M Comm



回答2:

For http (and not ssl protocol such as https ) request, you must use from OAthu 1.0a authenticate framework. There are many libraries for oauth 1.0a in java, i'm use from scribeJava

So, do the following steps:

  1. In app/build.gradle in dependency scop add this:

    compile 'org.scribe:scribe:1.3.5'

  2. New class as WoocommerceApi for provider of OAuth service. important. You must use a
    public class in DefaultApi10a for implementing oauth provider

    public static class WooCommerceApi extends org.scribe.builder.api.DefaultApi10a {
    
        @Override
        public org.scribe.model.Verb getRequestTokenVerb()
        {
            return org.scribe.model.Verb.POST;
        }
    
        @Override
        public String getRequestTokenEndpoint() {
            return "http://www.your-domain.com/wc-auth/authorize";
        }
    
        @Override
        public String getAccessTokenEndpoint() {
            return "none";
        }
    
        @Override
        public String getAuthorizationUrl(org.scribe.model.Token requestToken) {
            return "none";
        }
    }
    
  3. And you Must make request in Thread or AsyncTask

    String restURL = "http://www.your-domain.com/wp-json/wc/v1/products/";
    OAuthService service = new ServiceBuilder()
         .provider(WooCommerceApi.class)
         .apiKey(CONSUMER_KEY) //Your Consumer key
         .apiSecret(CONSUMER_SECRET) //Your Consumer secret
         .scope("API.Public") //fixed
         .signatureType(SignatureType.QueryString)
         .build();
    OAuthRequest request = new OAuthRequest(Verb.GET, restURL);
    Token accessToken = new Token("", ""); //not required for context.io
    service.signRequest(accessToken, request);
    Response response = request.send();
    Log.d("OAuthTask",response.getBody());
    


回答3:

According to the documentation the expected data format is JSON only (in contrast with the previous XML or Json) but there is unfortunately no further explanation on which data structure is expected for each endpoint.

Here's the only example of a POST request format from the current documentation for creating a coupon:

REST request URI

POST http://private-anon-0fe404a22-woocommercev2.apiary-mock.com/coupons?fields=id,code&filter=filter[limit]=100&page=2

Java code (pasted from the documentation)

Client client = ClientBuilder.newClient();
Entity payload = Entity.json("{  'coupon': {    'code': 'autumn-is-coming',    'type': 'fixed_cart',    'amount': '4.00',    'individual_use': true,    'description': ''  }}");
Response response = client.target("http://private-anon-0fe404a22-woocommercev2.apiary-mock.com")
  .path("/coupons{?fields,filter,page}")
  .request(MediaType.APPLICATION_JSON_TYPE)
  .post(payload);

System.out.println("status: " + response.getStatus());
System.out.println("headers: " + response.getHeaders());
System.out.println("body:" + response.readEntity(String.class));

Json response

{
  "coupon": {
    "id": 21548,
    "code": "augustheat",
    "type": "fixed_cart",
    "created_at": "2014-08-30T19:25:48Z",
    "updated_at": "2014-08-30T19:25:48Z",
    "amount": "5.00",
    "individual_use": false,
    "product_ids": [],
    "exclude_product_ids": [],
    "usage_limit": null,
    "usage_limit_per_user": null,
    "limit_usage_to_x_items": 0,
    "usage_count": 0,
    "expiry_date": "2014-08-30T21:22:13Z",
    "apply_before_tax": true,
    "enable_free_shipping": false,
    "product_category_ids": [],
    "exclude_product_category_ids": [],
    "exclude_sale_items": false,
    "minimum_amount": "0.00",
    "maximum_amount": "0.00",
    "customer_emails": [],
    "description": "Beat the August heat with $5 off your purchase!"
  }
}

http://docs.woocommercev2.apiary.io/#reference/coupons/coupons-collection/create-a-coupon

Considering the API is claimed to be accepting POST requests for all relevant endpoints this should be possible with a shopping order.