Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I am implementing Woo Commerce Rest API in my Angular/Ionic project on Cordova Platform. While I am making $http request to get product list or any other data, I am getting error Message. Here is my Service code:

angular.module('services.serverRepo', [])
.service("serverRepo", 
['$q','$http','errorHandler','$ionicLoading',function($q,$http,errorHandler,$ionicLoading){

var baseUrl="www.abc.com/wc-api/";
var self=this;
this.products=function(){
    var deff=$q.defer();
    $http({
        method:"GET",
        url:baseUrl+"v3/products",
        headers: {
                    "Content-Type":"application/JSON",
                    "oauth_consumer_key":"gjdfjkbgbdhh645h6bh456b45hbhbgdfhgbdfhgbdfhgbhgbdhfghhfhf",
                    "consumer_secret":"cs_97d74bbf5e9052ee053a05cbb1a53eec19c0847c"
                }
        }).then(function(objS){
            alert('Success :-    '+JSON.stringify(objS));
        },function(objE){
            alert('error:-    '+objE);
            errorHandler.serverErrorhandler(objE);
            deff.reject("server Error");
        });
        return deff.promise;
    };
}])
.service('errorHandler',['$q',function($q){
    this.serverErrorhandler=function(error){
        alert("ERROR ::"+JSON.stringify(error));
        console.log("ERROR ::"+JSON.stringify(error));
    };
 }
])

and in my controller.js file code is:

$scope.rentaldeptt = function(){
    //$ionicHistory.clearCache();
        serverRepo.products().then(function(objS){

                                   },function(err){
        });
}

I am calling $scope.rentaldeptt on a button click. In response I am getting error message

{"data":{"errors":[{"code":"woocommerce_api_authentication_error","message":"oauth_timestamp parameter is missing"}]},"status":404,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"url":"www.abc.com/v3/products","headers":{"Accept":"application/json, text/plain, /"},"params":{"oauth_consumer_key":"gjdfjkbgbdhh645h6bh456b45hbhbgdfhgbdfhgbdfhgbhgbdhfghhfhf","consumer_secret":"cs_97d74bbf5e9052ee053a05cbb1a53eec19c0847c"}},"statusText":"Not Found"}

Any Idea what I am doing wrong?

share|improve this question
    
are you got solution ? – Santosh Shinde Mar 9 at 10:45
    
No, Still I did not get answer for this question – Anjaney Mishra May 7 at 11:52
    
thank you for your replay, I got solution – Santosh Shinde May 8 at 8:05
1  
It would be great if you post over here.. – Anjaney Mishra May 9 at 4:44
    
please check the answer is this right – Santosh Shinde May 26 at 5:00

Please following steps to get answer,

Here I am Creating the service in angularjs to handle the calling of woocommerce api ,

            angular.module('myapp.restservices', [])
            .service("serverRepo",['$q','$http','errorHandler','$ionicLoading',function($q,$http,errorHandler,$ionicLoading){
                var self=this;
                 //Request Url and method
                var request = {
                        url: 'http://www.example.com/wc-api/v3/products',
                        method: 'GET'
                };  
                //OAuth Protocol authentication parameters
                var oauth = new OAuth({
                        consumer: {
                            //Consumer Public Key
                            public: 'ck_50xxxx',
                            //Consumer Secrete Key
                            secret: 'cs_b4xxxx'
                        },
                        //oauth1.0a protocol signature method
                        signature_method: 'HMAC-SHA1'
                });

                //Service Function to get products
                this.products=function(){
                     $ionicLoading.show({
                            template: '<ion-spinner class="light"></ion-spinner>'
                     });
                     //OAuth Parameters to call woocommerce api
                    var oauth_data = {
                        oauth_consumer_key: oauth.consumer.public,
                        oauth_nonce: oauth.getNonce(),
                        oauth_signature_method: oauth.signature_method,
                        oauth_timestamp: oauth.getTimeStamp()
                    };
                    //Oauth signature
                    oauth_data.oauth_signature = oauthSignature.generate(request.method,request.url,oauth_data,oauth.consumer.secret );
                    console.log("Params : "+ JSON.stringify(oauth_data));
                    var deff=$q.defer();
                    $http({
                        method:"GET",
                        url:request.url,
                        headers: {
                                    "Content-Type":"application/JSON",
                                },
                        params: oauth_data
                        }).then(function(objS){
                            $ionicLoading.hide();
                            alert('Success :-    '+JSON.stringify(objS));
                        },function(objE){
                            $ionicLoading.hide();
                            alert('error:-    '+JSON.stringify(objE));
                            errorHandler.serverErrorhandler(objE);
                            deff.reject("server Error");
                        });
                        return deff.promise;
                    };
                }])
                .service('errorHandler',['$q',function($q){
                    this.serverErrorhandler=function(error){
                        alert("ERROR ::"+JSON.stringify(error));
                        console.log("ERROR ::"+JSON.stringify(error));
                    };
                 }
            ])

Write controller to call the service function as like follows,

    angular.module(myapp.categorycontrollers, [])
    .controller('MainCtrl', function($scope,woocommerce) {
            //Method to get all Products
              $scope.getAllProducts = function(){
                    woocommerce.products().then(function(objS){
                     },function(err){
                    });
              }
             //calling to function
              $scope.getAllProducts();
     }

Hopes this will help you !

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.