angular 6 ошибка аутентификации REST woocommerce при обновлении пользователя (метод PUT не работает) - PullRequest
0 голосов
/ 09 октября 2018

Обновить код адреса

    savesddress() {
    this.presentLoading();
    let customerData = {
      customer: {}
    };
    customerData.customer = {
      "first_name": this.User.billing.first_name,
      "last_name": this.User.billing.last_name,
      "billing": {
        "first_name": this.User.billing.first_name,
        "last_name": this.User.billing.last_name,
        "company": this.User.billing.company,
        "address_1": this.User.billing.address_1,
        "address_2": this.User.billing.address_2,
        "city": this.User.billing.city,
        "postcode": this.User.billing.postcode,
        "country": this.User.billing.country,
        "email": this.User.billing.email,
        "phone": this.User.billing.phone
      }
    }

    console.log(this.User.id);
    console.log(customerData.customer);

    this.params = {}
    let producturl:string = this.woocommerce.authenticateApi('PUT','http://localhost/wp-json/wc/v2/customers/' +this.User.id+'',this.params);

    this.http.put(producturl,customerData.customer).subscribe(
      async (data:any) => {
      console.log(data);
      this.hideloader();
      const alert = await this.alertCtrl.create({
        header: 'Your address has been updated..',
        buttons: [{
          text: "OK",
          handler: () => {

            this.router.navigate(['AddressPage', { 'UserID': this.User.id }]);
          }
        }],
        cssClass: 'alert-success'
      });

      await alert.present();

    }, (err) => {
      console.log(err);
      this.hideloader();
    });
  }

Служба аутентификации

   export class WoocommerceService {

  woocommerce:any;
  nonce: string = ''
  currentTimestamp: number = 0 
  customer_key: string = 'ck_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; 
  customer_secret: string = 'cs_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';  
  constructor() { }

  authenticateApi(method,url,params) {
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    this.nonce ='';
    for(var i = 0; i < 6; i++) {
        this.nonce += possible.charAt(Math.floor(Math.random() * possible.length));
    }    
    this.currentTimestamp = Math.floor(new Date().getTime() / 1000);

    let authParam:object ={
        oauth_consumer_key : this.customer_key,
        oauth_nonce : this.nonce,
        oauth_signature_method : 'HMAC-SHA1',
        oauth_timestamp : this.currentTimestamp,
        oauth_version : '1.0',
    } 
    let parameters = Object.assign({}, authParam, params);
    let signatureStr:string = '';
    Object.keys(parameters).sort().forEach(function(key) {
        if(signatureStr == '')
            signatureStr += key+'='+parameters[key];
        else
            signatureStr += '&'+key+'='+parameters[key];
    });
    let paramStr:string = '';
    Object.keys(params).sort().forEach(function(key) {

        paramStr += '&'+key+'='+parameters[key];
    });
    return url+'?oauth_consumer_key='+this.customer_key+'&oauth_nonce='+this.nonce+'&oauth_version=1.0&oauth_signature_method=HMAC-SHA1&oauth_timestamp='+this.currentTimestamp+'&oauth_signature='+CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA1(method+'&'+encodeURIComponent(url)+'&'+encodeURIComponent(signatureStr),this.customer_secret+'&'))+paramStr;
}}

Это мой код, и я получил эту ошибку

{"code": "woocommerce_rest_authentication_error "," message ":" Неверная подпись - предоставленная подпись не совпадает. "," data ": {" status ": 401}}

запрос на получение работает нормально, у меня проблемы с созданиеми обновите пользователя

можете ли вы предложить любое решение для моих проблем

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...