2016-02-04 99 views
0

我試圖通過Woocommerce API對AngularJS和Ionic App進行身份驗證。這是我的代碼。Woocommerce API身份驗證錯誤

var ck = 'ck_6fffb5cbc00eqe00a7c3062cd85dae2ee64f919e'; 
     var cs = 'cs_c2fd467cqq8857a08941c15bc708e3c16dc1f026'; 
     var url = 'http://www.mysitecom/wc-api/v3'; 

     var httpMethod = 'GET', 
     parameters = { 
      oauth_consumer_key : ck, 
      oauth_token : '370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb', 
      oauth_nonce : 'kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg', 
      oauth_timestamp : new Date().getTime(), 
      oauth_signature_method : 'HMAC-SHA1', 
      oauth_version : '1.0'//, 
      //file : 'vacation.jpg', 
      //size : 'original' 
     }, 
     consumerSecret = cs, 
     tokenSecret = 'pfkkdhi9sl3r4s00'; 

     var signature = oauthSignature.generate(httpMethod, url, parameters, cs, tokenSecret, { encodeSignature: true }); 
     console.log(signature); 

     $http.get(url+'/products?oauth_consumer_key='+ck+'&oauth_nonce='+parameters.oauth_nonce+'&oauth_signature='+signature+'&oauth_signature_method=HMAC-SHA1&oauth_timestamp='+parameters.oauth_timestamp+'&oauth_token='+parameters.oauth_token+'oauth_version=1.0').then(function(data){ 
      console.log(data); 
     }, function(error){ 
      console.log(error); 
     }); 

我總是得到錯誤 Error Screenshot

PS:我正在使用this JavaScript庫來生成簽名。

回答

0

我做到了。第一天我就這麼接近,不知何故錯過了它。

var ck = 'ck_abcdefghijklmnopqrstuvwxyz'; 
    var cs = 'cs_zyxwvutsrqponmlkjihgfedcba'; 
    var url = 'http://samplesite.com/wc-api/v3/products'; 

    var oauth = OAuth({ 
     consumer: { 
      public: ck, 
      secret: cs 
     }, 
     signature_method: 'HMAC-SHA1' 
    }); 

    var request_data = { 
     method: 'get', 
    } 

    var params = oauth.authorize(request_data); 
    console.log(params); 

    $http.get(url+'/?oauth_signature='+ params.oauth_signature+ '&oauth_consumer_key='+ck+'&oauth_nonce='+params.oauth_nonce+'&oauth_signature_method=HMAC-SHA1&oauth_timestamp='+params.oauth_timestamp+'&oauth_token='+params.oauth_token+'&oauth_version=1.0') 
    .then(function(data){ 
     console.log(data); 
     $scope.products = data.data.products; 
    }, function(error){ 
     console.log(error); 
    }); 

不要忘了執行OAuth的庫here

+0

您是否有完整的信息源給我? –