Woocommerce API Authentication Error

2019-04-02 18:49发布

I am trying to authenticate as an AngularJS and Ionic App with Woocommerce API. Here is my code.

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

I am always getting the error Error Screenshot.

PS: I am using this javascript library to generate the signature.

1条回答
放我归山
2楼-- · 2019-04-02 19:35

I did it. I was so close to doing it on the first day, somehow missed it.

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

Don't forget to implement the OAuth library here.

查看更多
登录 后发表回答