我想使用来自以下网址的OAuth客户端通过GET请求从WooCommerce获取订单: Python OAuth WooCommerce
我的代码如下所示:
import time
from hashlib import sha1
import hmac
import binascii
from urllib import quote, urlencode
import httplib2
from collections import OrderedDict
import json
def key_compare(a, b):
return cmp(a[0], b[0])
class Restful_Client(object):
"""docstring for Restful_Client"""
def __init__(self, endpoint, consumer_key, consumer_secret):
super(Restful_Client, self).__init__()
self.consumer_key = consumer_key
self.consumer_secret = consumer_secret
self.endpoint = endpoint
def make_request(self, resource, params, method='GET' ):
oauth_params = {
'oauth_consumer_key': self.consumer_key,
'oauth_nonce': self.gen_nonce(),
'oauth_timestamp': self.gen_timestamp(),
'oauth_signature_method': 'HMAC-SHA1',
# 'oauth_version':'1.0'
}
oauth_params['oauth_signature'] = self.gen_signature(
resource,
OrderedDict( params.items() + oauth_params.items() ),
method
)
params = OrderedDict( params.items() + oauth_params.items() )
clean_params = self.sort_params(self.normalize_params(params))
uri = endpoint + resource
p_string = urlencode(clean_params)
return httplib2.Http().request(uri + '?' + p_string)
def gen_signature(self, resource, params, method):
base_request_uri = quote(self.endpoint + resource, safe = "")
clean_params = self.sort_params(
self.normalize_params(
self.normalize_params(
params
)
)
)
query_string = '%26'.join([
key + '%3D' + value\
for key, value in clean_params.iteritems()
])
raw_string = '&'.join([method, base_request_uri, query_string])
hashed = hmac.new(self.consumer_secret, raw_string, sha1)
return binascii.b2a_base64( hashed.digest() )[:-1]
def normalize_string(self, string):
return quote(str(string), safe="")
def normalize_params(self, params):
return OrderedDict( [
(self.normalize_string(key), self.normalize_string(value))\
for key, value \
in params.iteritems()
])
def sort_params(self, params):
return OrderedDict(sorted(
params.iteritems(),
cmp=key_compare
))
def gen_timestamp(self):
return int(time.time())
def gen_nonce(self):
return hex(self.gen_timestamp())
if __name__ == "__main__":
store_url = '<STORE URL HERE>'
api_base = 'wc-api'
api_ver = 'v3'
endpoint = "%s/%s/%s/" % (store_url, api_base, api_ver)
consumer_key = '<CK HERE>'
consumer_secret = '<CS HERE>&' # the & is necessary for api ver v3
resource = 'orders'
parameters = {}
rc = Restful_Client(endpoint, consumer_key, consumer_secret)
r, c = rc.make_request(resource, parameters, 'GET')
print json.dumps(r, indent=1)
c = json.loads(c)
print json.dumps(c, indent=1)
此代码导致WooCommerce发送10个最新订单,这些订单将存储在变量c中。在变量r中,有关于存储的请求的更多细节。这是变量r的示例输出:
{
"status": "200",
...,
"content-location": "<<STORE URL>/wc-api/v3/orders?oauth_consumer_key=<CK HERE>&oauth_nonce=0x560bc13c&oauth_signature=Ln6IbXciDSNjTfraKe1Cx%252FPRDkU%253D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1443610940",
"x-cache": "MISS",
"x-wc-total": "1180",
...,
"x-wc-totalpages": "118",
"-content-encoding": "gzip",
"link": "<<STORE URL>/wc-api/v3/orders?oauth_consumer_key=<CK HERE>&oauth_nonce=0x560bc13c&oauth_signature=Ln6IbXciDSNjTfraKe1Cx%2FPRDkU%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1443610940&page=2>; rel=\"next\", <<STORE URL>/wc-api/v3/orders?oauth_consumer_key=<CK HERE>&oauth_nonce=0x560bc13c&oauth_signature=Ln6IbXciDSNjTfraKe1Cx%2FPRDkU%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1443610940&page=118>; rel=\"last\"",
...,
"content-type": "application/json; charset=UTF-8",
"accept-ranges": "bytes"
}
与关键字“链接”相关联,链接有一些请求所基于的链接,并且可以看到订单请求是在不同的页面中发送的。显然,上面的代码总是请求最后一页。
我的问题:
代码中的哪个位置是指定的请求页面?
如何指定我想要请求的页面,如果可能,更改每页显示的订单数量?
非常感谢每一位帮助!
(我的编程经验有限。)