我正在开发一个移动应用程序,以使用离子Cordova平台连接到WooCommerce WordPress插件rest api。
但是尝试获取具有特定属性术语的产品时遇到错误。
我的要求是:
http://myurl.com/wc-api/v3/products?filter[pa_color]=red,black
我得到woocommerce_api_authentication_error
。
我发送到同一服务器的所有其他请求都可以正常工作,包括http://myurl.com/wc-api/v3/products?filter[pa_color]=black
是相同的请求,但只传递一个属性项。
我使用woocommerce版本3.5.3
科尔多瓦8.0.0
非常感谢您的帮助!
如果有帮助,我会共享我的源代码:
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { URLSearchParams } from '@angular/http';
import {HttpClient} from "@angular/common/http";
@Injectable()
export class CategoryService {
constructor(private http: Http, private config: Config,private HttpClient: HttpClient) {
}
load() {
var params ['filter[pa_color]'] = new Array('red,black');
return new Promise(resolve => {
this.http.get(this.config.setUrl('GET', '/wc-api/v3/products?', params), this.config.options).map(res => res.json())
.subscribe(data => {
resolve(data);
},
error=>{
console.log(error);
});
});
}
import { Injectable } from '@angular/core';
import { URLSearchParams } from '@angular/http';
import { Headers } from '@angular/http';
declare var oauthSignature: any;
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form- urlencoded; charset=UTF-8');
@Injectable()
export class Config {
url: any = 'https://myurl.com';
consumerKey: any = 'ck_myUrlCK';
consumerSecret: any = 'cs_ck_myUrlSK';
oauth: any;
signedUrl: any;
randomString: any;
oauth_nonce: any;
oauth_signature_method: any;
encodedSignature: any;
searchParams: any;
customer_id: any;
params: any;
options: any = {};
constructor() {
this.options.withCredentials = true;
this.options.headers = headers;
this.oauth = oauthSignature;
this.oauth_signature_method = 'HMAC-SHA1';
this.searchParams = new URLSearchParams();
this.params = {};
this.params.oauth_consumer_key = this.consumerKey;
this.params.oauth_signature_method = 'HMAC-SHA1';
this.params.oauth_version = '1.0';
}
setOauthNonce(length, chars) {
var result = '';
for (var i = length; i > 0; --i) result += chars[Math.round(Math.random() * (chars.length - 1))];
return result;
}
setUrl(method, endpoint, filter) {
var key;
var unordered = {};
var ordered = {};
if (this.url.indexOf('https') >= 0) {
unordered = {};
if (filter) {
for (key in filter) {
unordered[key] = filter[key];
}
}
unordered['consumer_key'] = this.consumerKey;
unordered['consumer_secret'] = this.consumerSecret;
Object.keys(unordered).sort().forEach(function(key) {
ordered[key] = unordered[key];
});
this.searchParams = new URLSearchParams();
for (key in ordered) {
this.searchParams.set(key, ordered[key]);
}
return this.url + endpoint + this.searchParams.toString();
}
else {
var url = this.url + endpoint;
this.params['oauth_consumer_key'] = this.consumerKey;
this.params['oauth_nonce'] = this.setOauthNonce(32, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
this.params['oauth_timestamp'] = new Date().getTime() / 1000;
for (key in this.params) {
unordered[key] = this.params[key];
}
if (filter) {
for (key in filter) {
unordered[key] = filter[key];
}
}
Object.keys(unordered).sort().forEach(function(key) {
ordered[key] = unordered[key];
});
this.searchParams = new URLSearchParams();
for (key in ordered) {
this.searchParams.set(key, ordered[key]);
}
this.encodedSignature = this.oauth.generate(method, url, this.searchParams.toString(), this.consumerSecret);
return this.url + endpoint + this.searchParams.toString() + '&oauth_signature=' + this.encodedSignature;
}
}
}