美文网首页PythonPython3 网络爬虫首页推荐
Python3+Flask+淘宝OPENapi SDK 调用淘宝

Python3+Flask+淘宝OPENapi SDK 调用淘宝

作者: 大华日记 | 来源:发表于2018-05-07 10:30 被阅读274次

For Example:

/gxapi?session=6101e11da8f3e7596331c7d929076e7960e337657ec204a82248874&item_id=552365633968&adzone_id=67754887&site_id=17832334

response:

{"tbk_privilege_get_response":{"result":{"data":{"category_id":16,"coupon_click_url":"https:\/\/uland.taobao.com\/coupon\/edetail?e=sk6A1xcPQKgGQASttHIRqTaGt2Gr6vc%2FWnPJTYHrkEC4g5bpOht9iozu9PDeDChJbVlJP6GczYBzNAvPFp2mUJBh%2BsFgnewC0TK%2FwLyqPyXYhpVVy38fp0MuxoRQ3C%2BHIRf%2BWIKeYeuie%2FpBy9wBFg%3D%3D&traceId=0ab290ae15256592243493721e","item_id":552365633968,"max_commission_rate":"5.50"}},"request_id":"11igeytvupvok"}}
import time
from flask import Flask
import urllib
import hashlib
import urllib.parse
import urllib.request
from flask import request

app = Flask(__name__)

app_key = ''
appSecret = ''


def ksort(d):
    return [(k, d[k]) for k in sorted(d.keys())]

def md5(s, raw_output=False):
    """Calculates the md5 hash of a given string"""
    res = hashlib.md5(s.encode())
    if raw_output:
        return res.digest()
    return res.hexdigest()


def createSign(paramArr):
    sign = appSecret
    paramArr = ksort(paramArr)
    paramArr = dict(paramArr)
    print(paramArr)
    for k, v in paramArr.items():
        if k != '' and v != '':
            sign += k + v
    sign += appSecret
    print(sign)
    sign = md5(sign).upper()
    return sign


def createStrParam(paramArr):
    strParam = ''
    for k, v in paramArr.items():
        if k != '' and v != '':
            strParam += k + '=' + urllib.parse.quote_plus(v) + '&'
    return strParam

#如需固定API 可在下方数组内加入. 如'method'=> 'taobao.tbk.privilege.get'
paramArr = {'app_key': app_key, 'v': '2.0', 'sign_method': 'md5', 'format': 'json',
            'timestamp': time.strftime('%Y-%m-%d %H:%M:%S'), 'method': 'taobao.tbk.privilege.get'}
print(paramArr)

@app.route('/')
def hello_world():
    abc = "For Example:gxapi?session=6101e11da8f3e7596331c7d929076e7960e337657ec204a82248874&item_id=552365633968&adzone_id=67754887&site_id=17832334"
    abc += "<br>" +"Get session:https://oauth.taobao.com/authorize?response_type=token&client_id=23421352&state=mrtk&view=web"

    return abc

@app.route('/gxapi', methods={'GET', 'POST'})
def gxapi():

    global paramArr
    paramArr = {**paramArr , **request.args.to_dict()}
    sign = createSign(paramArr)
    strParam = createStrParam(paramArr)
    strParam += 'sign=' + sign
    url = 'http://gw.api.taobao.com/router/rest?'+ strParam
    print(url)
    res = urllib.request.urlopen(url).read()
    return res

if __name__ == '__main__':
    app.run(debug=True)

相关文章

网友评论

本文标题:Python3+Flask+淘宝OPENapi SDK 调用淘宝

本文链接:https://www.haomeiwen.com/subject/avrmrftx.html