您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關python,Django實現的淘寶客登錄功能,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
參考代碼如下:
#!/usr/bin/python #coding:utf-8 import datetime, urllib, base64, random from hashlib import md5 from django.conf import settings from django.contrib import auth from django.contrib.auth.models import User from django.core.urlresolvers import reverse from django.http import HttpResponseRedirect from django.shortcuts import render from app.taobao.backends import TaobaoBackend from app.taobao.models import User as taobao_user from app.taobao.forms import UserOnceChange from pyapi.taobao.top import Request def authenticated(request, template): if request.method == 'POST': form = UserOnceChange(request=request, data=request.POST) if form.is_valid(): request.user.username = form.cleaned_data['username'] password = form.cleaned_data.get('password', None) if password: request.user.set_password(form.cleaned_data) request.user.save() #更新應用用戶綁定 tb_user = request.user.taobao.all()[0] tb_user.has_bind_user=True tb_user.save() return HttpResponseRedirect(reverse('user')) top_session = request.GET.get('top_session', None) top_parameters = request.GET.get('top_parameters', None) top_sign = str(request.GET.get('top_sign', None)) #解析 top parameters if top_parameters: back_parameters = base64.decodestring(urllib.unquote(top_parameters)) infos = dict(k.split("=") for k in back_parameters.split("&")) username = infos.get('visitor_nick', None) visitor_id = infos.get("visitor_id", None) # validate sign 認證返回值中的簽名 local_sign_str = '%s%s%s%s' % (settings.TAOBAO_APP_KEY, top_parameters, top_session, settings.TAOBAO_APP_SECRET) local_sign = base64.encodestring(md5(local_sign_str).digest()) #注意 base64 encode后,字符串最后會有 \n 符, 因此需要清除才能驗證 if top_sign == local_sign.strip(): # 清除 \n # 創建/返回淘寶賬戶 tb_user, tb_user_created = taobao_user.objects.get_or_create(id=int(visitor_id), nick=username) #建立系統用戶, 用戶名存在則創建隨機數擴展 if tb_user.user_id is None: try: User.objects.get(username=username) django_user_username = username tb_user.has_bind_user = True except: django_user_username = '%s_%s' % (username, random.randint(1000, 10000)) django_user, django_user_created = User.objects.get_or_create(username=django_user_username) if django_user_created: django_user.set_password(visitor_id) django_user.save() tb_user.user = django_user # 保存返回的信息:過期時間 token ts等 tb_user.expires_in = datetime.datetime.now() + datetime.timedelta(seconds=int(infos.get("expires_in", 0))) tb_user.ts = infos.get("ts", '') tb_user.refresh_token = infos.get("refresh_token", '') tb_user.save() # update user profile in taobao auth backend user = auth.authenticate(id=tb_user.id, nick=tb_user.nick) if user: auth.login(request, user) #如果應用賬戶沒有綁定系統用戶,則用戶第一次時可以修改用戶名 if not tb_user.has_bind_user: form = UserOnceChange(request=request, initial={'username':tb_user.user.username, 'password':tb_user.id}) return render(request, template, locals()) return HttpResponseRedirect(settings.TAOBAO_AUTH_URL)
以上就是python,Django實現的淘寶客登錄功能,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。