F
F
FlyToDream2016-06-19 18:04:14
Apache Cordova
FlyToDream, 2016-06-19 18:04:14

How to connect Ionic Framework and woocommerce api?

Such problem:
I create application under Ionic + WC-API.
When you try to access the goods, it says not a valid key. Created a key in Woocommerce Admin. On the Internet, I found that authentication is also needed.
It seems that there is a working code, but I paste the code and it does not work (the state simply does not open):
stackoverflow.com/questions/35889338/call-woocommerce-2-1-rest-apt-in-ionic-framework-using-ouath1-a
santoshshinde2012 .blogspot.ru/2016/03/woocommerce-rest-api-with-ionic.html#comment-form I
set my own keys.
I downloaded these libraries and included them in index.html:
hmac-sha1.js
enc-base64.js
oauth-1.0a.js
oauth-signature.js

angular.module(myapp.restservices', [])
.service("woocommerce",['$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_50xxxxxxxx',
        //Consumer Secrete Key
                                          secret: 'cs_b5xxxxx'
                        },
          //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:request.method,
                      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));
        };
     }
])

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();
 }

But still doesn't work. How to get a list of products sitename.ru/wc-api/v3/products and pass this authentication. Debug application in VisualStudio 2015 (there is a local server with Internet access). When I did the output of posts through the WordPress API, everything works fine, but there is such a problem with WC.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question