4

I am trying to create an order from iOS to woo commerce using Alamofire. I am searching for a proper solution.

After trying to create an order I get this error:

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

Code:

let parameters: [String: AnyObject] = [
    "consumer_key":"*******" as AnyObject, // here is my user name
    "consumer_secret":"*******" as AnyObject, // here is my secret key

    "shipping_total": "120.00" as AnyObject,
    "total": "6015.00" as AnyObject,
    "customer_id": 0 as AnyObject,
    "billing": [
        "first_name": "Faizul",
        "last_name": "karim",
        "company": "somecompany",
        "address_1": "someAddress",
        "address_2": "someAddress",
        "city": "Dhaka",
        "state": "Dhaka",
        "postcode": "1203",
        "country": "bd",
        "email": "faizulkarim28@gmail.com",
        "phone": "001929838939"
        ] as AnyObject,
    "shipping": [
        "first_name": "Faizul",
        "last_name": "karim",
        "company": "somecompany",
        "address_1": "someAddress",
        "address_2": "someAddress",
        "city": "Dhaka",
        "state": "Dhaka",
        "postcode": "1203",
        "country": "bd",
    ] as AnyObject,
    "line_items":[
        "id": 388,
        "name": "Mens Casual Blazer - 40",
        "product_id": 55677,
        "variation_id": 57619,
        "quantity": 1,
        "tax_class": "",
        "subtotal": "5895.00",
        "subtotal_tax": "0.00",
        "total": "5895.00",
        "total_tax": "0.00",
    ] as AnyObject
]

Alamofire.request("https://infinitymegamall.com/wp-json/wc/v2/orders",method: .post,  parameters: parameters)
    .responseJSON{  response in
        if let json = response.result.value {
            print(json)
        }
    }
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Faizul Karim
  • 333
  • 2
  • 13
  • I've never worked with the woocommerce API, but it seems like you are not using proper authentication methods, hence the error. – Dávid Pásztor Jan 21 '18 at 23:51
  • i am able to get Json data with this auth. but unable to post data. in get data there war only 2 parameter value. but when i try to post order there is many parameter Value. – Faizul Karim Jan 21 '18 at 23:58
  • Looking at the [documentation](https://docs.woocommerce.com/document/woocommerce-rest-api/) of the API, it seems like you need to specify rights for the API key, are you sure you set up write access as well, not just read? – Dávid Pásztor Jan 22 '18 at 00:00
  • i am sure that access code is fine – Faizul Karim Jan 22 '18 at 20:20
  • I did this before android. There are things missing in the parameter. The most important is oauth_signature. To do this, hmac needs to encrypt with sha1, but I can not confirm this encryption anyway. – Umut ADALI Jan 25 '18 at 12:21
  • What's your api permissions in wordpress?, and may be try to send the authentication parameters in headers. At first sight it looks like you need to add write permissions to your user or have first an access token – Camilo Lopez Jan 31 '19 at 10:49

1 Answers1

0

Are you sure your API Keys have write permissions? If GET works, it's possible that the key allows read only. Double check the permissions in your Woocommerce API Settings.