Ionic with woocommerce Rest API


#1

Hi,

I develop a new Project using Wordpress Rest API.

I refer this https://www.npmjs.com/package/oauth-1.0a.

I installed


  npm install oauth-1.0a --production

  npm install crypto-js

Here is my code

 import OAuth from 'oauth-1.0a'
import CryptoJS from 'crypto-js';

export class ApiProvider {
  oauth: any;
  authkey:any;
  keyoauth:any;
  apiconstant;
  constructor(public http: Http,public Events:Events,public storage: Storage) {
    console.log('Hello ApiProvider Provider');
    this.apiconstant={ 
      baseurl: 'http://coitor.com/emke/',
      apiUrl: 'http://coitor.com/emke/wc-api/v3/',
      consumerkey: 'ck_2b9a76417280178b642975df59aaaf9ebf816247',
      consumersecret: 'cs_a0b51e4ced110bc08180824bcdee6dc1bf498341'
    };

  }

 //Get Categories

  getcategories()
  {
    //OAUTH
    
    function hash_function_sha1(base_string, key) {
        return CryptoJS.HmacSHA1(base_string, key).toString(CryptoJS.enc.Base64); 
        //return crypto.createHmac('sha1', key).update(base_string).digest('base64');
    }
    this.oauth = new OAuth({
        consumer: {
            key: this.apiconstant.consumerkey,
            secret: this.apiconstant.consumersecret
        },        
        hash_function: hash_function_sha1,
        signature_method: 'HMAC-SHA1'
    });  

    var request_data = {
        url: this.apiconstant.apiUrl+'products/categories',
        method: 'GET'
    };

    this.authkey = this.oauth.authorize(request_data);
    this.keyoauth = new URLSearchParams();
    for(let param in this.authkey) {
        this.keyoauth.set(param, this.authkey[param]);
    }
    console.log(this.keyoauth);
    let options = new RequestOptions({
        method: request_data.method,
        url: request_data.url,
       headers: this.oauth.toHeader(this.oauth.authorize(request_data)),
       search: this.keyoauth
    });

    return this.http.get(this.apiconstant.apiUrl+'products/categories',options)
    .map(res => res.json());
   
  }

Here is my output

OPTIONS http://coitor.com/emke/wc-api/v3/products/categories?oauth_consumer_key=ck_…=1501734186&oauth_version=1.0&oauth_signature=wsM+lvma2154IOlEWbhgGQJNcv0= 401 (Unauthorized)

But the same URL copy and ran the browser I got Output.

How can i fix this issue.

Kindly advice me,

Thanks


#2

Your app makes an OPTIONS request. Your browser probably did a GET request. These are different. This is all because of CORS and browser security, no real workaround.

If you are testing in browser, you might be able to use a Service Proxy in the CLI to work around it though: https://github.com/ionic-team/ionic-cli#service-proxies


#3

Thanks for ur reply.why I am getting Unauthorized error.


#4

Because your server probably doesn’t handle the OPTIONS request the correct way.


#5

Hi,

This API only work Browser.Does not support mobile.

I got 404 found error message.Same time I copied URL and ran browser i got output.

How can i fix this issue.

Is a server problem or coding problem ?

I dont understand.

Kindly advice me,

Thanks.