3

I am using alamofire for networking in swift3.0 project. I need to get data from woocommerce rest apis e.g http://woocommerce.github.io/woocommerce-rest-api-docs/#product-properties Below is the code I have added in my project. I think there is an authentication issue.

let params = ["oauth_consumer_key":consumerKey, "oauth_consumer_secret":consumerSecret, "oauth_timestamp":timeInterval, "oauth_nonce": nonce, "oauth_signature_method": "HMAC-SHA1", "oauth_version": "1.0"] as [String : Any];

    Alamofire.request(url, parameters: params)
        .responseJSON { response in
            print(response.request)  // original URL request
            print(response.response) // HTTP URL response
            print(response.data)     // server data
            print(response.result)   // result of response serialization

            if let JSON = response.result.value {
                print("JSON: \(JSON)")
            }
    }

Response:

{
    code = "woocommerce_rest_cannot_view";
    data =     {
        status = 401;
    };
    message = "Sorry, you cannot view this resource.";
}
Mubeen Qazi
  • 167
  • 1
  • 8
  • After wasting approx 2-3 days I figured a solution and that is to add simple Php library which handle all the request by it self. Library : https://github.com/kloon/WooCommerce-REST-API-Client-Library I should have thought in this direction 2-3 days earlier! – Mubeen Qazi Nov 15 '16 at 14:36
  • P.S: The code posted in question is not a correct way to communicate oauth1 apis. I have tried many different ways to create oauth signature by base url (Method&URL&Parameter) and consumer secrect and hashing them by SHA1. https://oauth1.wp-api.org/docs/basics/Signing.html – Mubeen Qazi Nov 15 '16 at 14:39
  • Do you have found a solution, except the library php ? Is it impossible to do it only with alamofire ? Cause i got the same problem... – Makaille Jan 20 '17 at 09:55
  • I would not say its impossible but finding the solution for that and to know whether you are trying in right direction is difficult because of Outh1.0 conventional ways of authentication. – Mubeen Qazi Jan 21 '17 at 15:06
  • I agree, thank's for your response. I've found a solution, using OAuthSwiftAlamofire pod to sign my request. – Makaille Jan 24 '17 at 13:18
  • Good to hear that. Can you post the solution with some details/code so other can benefit from it.thanks – Mubeen Qazi Jan 26 '17 at 09:37
  • I think this is really specific to me, but when i've the time i'll do it – Makaille Jan 27 '17 at 09:13
  • Use https://github.com/OAuthSwift/OAuthSwiftAlamofire this library. This solved my issue. You don't have to pass all those values. Just add consumer_secret and consumer_key using this library and it will work fine. – Ajith kumar Jul 05 '18 at 06:52

0 Answers0