[英]WooCommerce API for mobile App
我計划為WooCommerce商店開發原生移動Android應用程序。
我在這里查看了他們的REST API文檔: http : //docs.woocommercev2.apiary.io/我已經開始測試它但是當我做不同的調用時
GET /orders
讓我們說它會返回商店的所有訂單。
有誰知道如何使用他們的API開發最終用戶應用程序。
例如:
GET /products
PUT /order
(為登錄用戶創建訂單)
GET /order
(獲取登錄用戶的訂單)
任何想法都贊賞:)
提前致謝。
對於http
(而不是諸如https
ssl協議)請求,您必須使用OAthu 1.0a身份驗證框架。 java中的oauth 1.0a有很多庫,我在scribeJava中使用
因此,請執行以下步驟:
在依賴scop的app/build.gradle
中添加:
compile 'org.scribe:scribe:1.3.5'
作為OAuth服務提供商的WoocommerceApi新類。 重要。 你必須使用
DefaultApi10a中的public
類,用於實現oauth提供程序
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"; } }
並且您必須在Thread
或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); // for POST requests // OAuthRequest request = new OAuthRequest(Verb.POST, restURL); // request.addBodyParameter(YOUR_PARAM_KEY, YOUR_VALUE); // or // request.addPayload(YOUR_JSON); Token accessToken = new Token("", ""); //not required for context.io service.signRequest(accessToken, request); Response response = request.send(); Log.d("OAuthTask",response.getBody());
根據文檔,預期的數據格式僅為JSON(與之前的XML或Json形成對比),但遺憾的是,沒有進一步解釋每個端點的數據結構。
以下是當前創建優惠券文檔中POST請求格式的唯一示例:
REST請求URI
POST http://private-anon-0fe404a22-woocommercev2.apiary-mock.com/coupons?fields=id,code&filter=filter[limit]=100&page=2
Java代碼(從文檔中粘貼)
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回應
{
"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
考慮到聲稱API接受所有相關端點的POST請求,這應該可以通過購物訂單進行。
我會建議這個步驟
首先,您可以從后端啟用針對woocommerce的Api - http://docs.woothemes.com/document/woocommerce-rest-api/
https://www.npmjs.com/package/woocommerce使用此鏈接,該鏈接具有與woocommerce交互的所有方法。 否則使用輕量級中間件,它有助於連接woocommerce服務器並將JSON數據返回到您的應用程序。
使用離子框架編寫服務並與您的瘦中間件客戶端交談。 不要忘記緩存數據(使用本地存儲),這樣就不會一直打到服務器。 - Contus M Comm
可以使用即插即用解決方案AKA App構建器(如Appmaker.xyz )來創建最終用戶應用程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.