How to use the "POST" Http Verb Woocommerce REST API in JAVA / Android?

I am developing an Android application for my WooCommerce store and retrieving storage data such as Products, Categories, Orders, Customers, etc. using the GET http verb WooCommerce REST Api . It works fine and I can correctly generate oAuth 1.0 signature for api V2 and V3. Now I want to perform the "Record" action. I found out from the same documentation that I need to use the POST Http verb. I tried the same and stuck.

When I perform any POST action with an HttpGet or HttpPost request using the URL, oAuth data and the generated signature, I get:

{"errors":[{"code":"woocommerce_api_authentication_error","message":"Invalid Signature - provided signature does not match"}]} 

I follow all the instructions given in the document and found on Google, also used the "POST" line to generate the oAuth signature, tried to send parameters using HttpGet and HttpPost, but could not.

Can someone please provide me some instruction or example to use the verb POST Http for Android to perform a write action using the WooCommerce REST API. (e.g. Create a new order, Create a new category, etc.)

+5
source share
1 answer

I encountered the same error and I needed to create another POST adapter class. I am using a modification for network calls, and here is my snippet code:

 package me.gilo.a55thavenue.data; import android.util.Base64; import android.util.Log; import com.squareup.okhttp.HttpUrl; import com.squareup.okhttp.Interceptor; import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.Request; import com.squareup.okhttp.Response; import org.apache.http.NameValuePair; import org.apache.http.client.utils.URLEncodedUtils; import org.apache.http.message.BasicNameValuePair; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import retrofit.GsonConverterFactory; import retrofit.Retrofit; import retrofit.RxJavaCallAdapterFactory; /** * Created by Aron on 10/31/2015. */ public class PostRestAdapter { static String oauth_nonce = ""; static String oauth_timestamp = ""; static String oauth_signature_method = "HMAC-SHA1"; static ArrayList<NameValuePair> params; public static API createAPI(final String endpoint) { setParams(endpoint); // Define the interceptor, add authentication headers Interceptor interceptor = new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { HttpUrl.Builder builder = chain.request().httpUrl().newBuilder(); for (NameValuePair entry : params) { builder.addQueryParameter(entry.getName(), entry.getValue()); } Request newRequest = chain.request() .newBuilder() .url(builder.build()) .build(); return chain.proceed(newRequest); } }; // Add the interceptor to OkHttpClient OkHttpClient client = new OkHttpClient(); client.interceptors().add(interceptor); Retrofit retrofit = new Retrofit.Builder() .baseUrl(API.BASE_URL) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .client(client) .build(); return retrofit.create(API.class); } public static ArrayList<NameValuePair> setParams(String endpoint) { final String uri = API.BASE_URL + endpoint; oauth_nonce = getOauth_nonce(); oauth_timestamp = getOauth_timestamp(); params = new ArrayList<>(); params.add(new BasicNameValuePair("oauth_consumer_key", API.CONSUMER_KEY)); params.add(new BasicNameValuePair("oauth_nonce", oauth_nonce)); params.add(new BasicNameValuePair("oauth_timestamp", oauth_timestamp)); params.add(new BasicNameValuePair("oauth_signature_method", oauth_signature_method)); Collections.sort(params, new SortParams()); String encodedParams = URLEncodedUtils.format(params, "utf-8"); Log.d("encodedParamString", encodedParams); String string_to_sign = ""; try { string_to_sign = (new StringBuilder("POST&")).append(URLEncoder.encode(uri, "utf-8")).append("&").append(URLEncoder.encode(encodedParams, "utf-8")).toString(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } Log.d("string to sign", string_to_sign); try { Mac mac = Mac.getInstance("HMAC-SHA1"); String secret = API.CONSUMER_SECRET; if (API.WP_API_VERSION.equals("3")) { secret = API.CONSUMER_SECRET + "&"; } mac.init(new SecretKeySpec(secret.getBytes("utf-8"), "HMAC-SHA1")); String signature = Base64.encodeToString(mac.doFinal(string_to_sign.getBytes("utf-8")), 0).trim(); Log.d("signature", signature); params.add(new BasicNameValuePair("oauth_signature", signature)); } catch (NoSuchAlgorithmException | InvalidKeyException | UnsupportedEncodingException e) { e.printStackTrace(); } return params; } public static String getOauth_nonce() { return (new StringBuilder(String.valueOf(Math.random() * 100000000D))).toString(); } public static String getOauth_timestamp() { long stamp = (long) (System.currentTimeMillis() / 1000D); Log.d("stamp", stamp + ""); return (new StringBuilder(String.valueOf(stamp))).toString(); } static class SortParams implements Comparator<NameValuePair> { @Override public int compare(NameValuePair nameValuePair1, NameValuePair nameValuePair2) { return nameValuePair1.getName().compareTo(nameValuePair2.getName()); } } } 

[Source: https://gist.github.com/Aroniez/41dbc5942f70641b397e]

+3
source

Source: https://habr.com/ru/post/1241471/


All Articles