django应用的淘宝API插件,实在觉得淘宝官方的不好用,用django-taobao插件可快速让django项目调用淘宝API
安装pipinstalldjango-taobao
配置目前我将淘宝授权的代码贡献到了pythonsocialauth.所以授权是需要安装python_social_auth并且简单配置django的settings.py:
INSTALLED_APPS = ( ... 'django_taobao', 'django_social_auth.apps.django_app.default',)AUTHENTICATION_BACKENDS = [ 'social.backends.taobao.TAOBAOAuth',]SOCIAL_AUTH_TAOBAO_KEY = 'your client id'SOCIAL_AUTH_TAOBAO_SECRET = 'your app secret' TAOBAO_API_URL ='https://gw.api.tbsandbox.com/router/rest'把social_auth加入到你的urls.py:
url(r'^taobao/', include('taobao.urls')), # this is taobao api exampleurl('', include('social.apps.django_app.urls', namespace='social')), # this is for auth现在可以到youhost.com/login/taobao登陆并取到token
使用API有了token以后,就可以使用API了.代码里的view.py其实就是个例子:
def user_info(request): print request.user.id user = get_object_or_404(UserSocialAuth.objects.filter(provider='taobao'),user=request.user) token = user.extra_data['access_token'] t = TaoBao() t.setParams(access_token=token, method='taobao.user.seller.get') t.setFields('nick,sex') return render_to_response("taobao/index.html", RequestContext(request, {'seller':t.fetch()}))
评论