0

I'm developing an android app with Woocommerce REST API. I 'm able to access the data's through this REST api using GET method,

now i'm facing issue in creating new customer using this REST API.

here POST method is not working.

my END_POINT is "http:example.com/wp-json/wc/v1/customers"

the problem is am getting authentication error.

I'm using OkHttp for network call.

Here is my code:

protected String doInBackground(Void... params) {
        try {
            MediaType JSON = MediaType.parse("application/json; charset=utf-8");
            String authHeader = Credentials.basic(Config.CONSUMER_KEY, Config.CONSUMER_SECRET);
            Log.e(TAG, "doInBackground: auth -> " + authHeader);
            RequestBody body = RequestBody.create(JSON, json);

            Request request = new Request.Builder()
                    .url(url)
                    .post(body)
                    .addHeader("Content-Type", "application/json; charset=utf-8")
                    .addHeader("Accept", "application/json")
                    .addHeader("Authorization", authHeader)
                    .build();

            OkHttpOAuthConsumer consumer = new OkHttpOAuthConsumer(Config.CONSUMER_KEY, Config.CONSUMER_SECRET);

            OkHttpClient client = new OkHttpClient.Builder()
                    .addInterceptor(new SigningInterceptor(consumer))
                    .build();

            Response response = client.newCall(request).execute();

            return response.message();
        } catch (IOException e) {
            e.printStackTrace();
            Log.e(TAG, "doInBackground: " + e.getLocalizedMessage());
            return Tag.IO_EXCEPTION;
        }
    }

Response message is :

{"code":"woocommerce_rest_cannot_create","message":"Sorry, you are not allowed to create resources.","data":{"status":401}}

i don't know where is an issue is.

If anyone experienced this problem, means please share your solution.

Thanks in advance.

Madhan
  • 495
  • 2
  • 10
  • 22
  • The returned message is clear "Sorry, you are not allowed to create resources.". Are you sure that you have permissions for create resources? Or using the correct REST API keys? – Claudio Sanches Aug 10 '16 at 15:28
  • @claudioSanches yes, i'm using correct end Point, and i have verified the permission for the key that i generated. the permission is read_write. – Madhan Aug 11 '16 at 04:41
  • @claudioSanches here i'm trying to create a new customer, i'm sure that this REST API reads my request body(which contains email and password in json format), If my credentials doesn't have enough permission to create a resource, it should not allow me to create resource right, but i'm able to create a new customer using the same REST API key with postman, and i'm not receiving any response like this one. – Madhan Aug 11 '16 at 04:52
  • Hi.. did you get a solution for this? I am having a same issue.. – Tripti Kumar Nov 05 '16 at 09:58
  • @iPhoneDeveloper till now i didn't found any solution for this error response. Finally i quit using this Woocommerce REST api and i build my own REST API for my project. I feel my own REST API is better than this woocommerce REST API because of this poor documentation(particularly for error response). – Madhan Nov 05 '16 at 14:49
  • Thanks..this is weird ..since all the other REST APIs are working well. I am getting error in only this one. – Tripti Kumar Nov 05 '16 at 14:57
  • @iPhoneDeveloper all POST method are not working in this REST API, anyway if you find solution for this issue, Please share it here. – Madhan Nov 05 '16 at 15:07
  • Anyone got any solution? – Hafiz Muneeb Feb 20 '18 at 08:14

0 Answers0