2016-03-09 39 views
1

我想在使用Ouath1.0a協議的離子移動應用程序中調用WooCommerce 2.1 REST APT。使用Ouath1.a在離子框架中調用WooCommerce 2.1 REST APT

例如:

http://www.example.com/wc-api/v3/products? 

要調用這個服務,我們需要像

oauth_consumer_key 
oauth_signature_method=HMAC-SHA1 
oauth_timestamp= xxxxxxxxxx 
oauth_nonce= xxxxxx 
oauth_version=1.0 
oauth_signature= ?????? 

現在我面臨的問題參數如何創建oauth_signature離子&角JS。

我指的link 1 & link2

回答

0

經過長期搜索我得到這個issue的幫助下解決方案。

解決方案如下:

我們所需要的一些依賴實現這個像下面這樣

  1. HMAC-SHA1
  2. ENC-的base64
  3. OAuth的1.0A
  4. oauth-signature

Dow n載入的js文件並將其放置在您需要的目錄,並在您的index.html

我在這裏創造了angularjs的服務來處理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)); 
        }; 
       } 
      ]) 

寫控制器調用服務功能如下,

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

欲瞭解更多幫助請致電here