欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python,Django實(shí)現(xiàn)的淘寶客登錄功能示例

 更新時間:2019年06月12日 10:27:18   作者:輕舞肥羊  
這篇文章主要介紹了python,Django實(shí)現(xiàn)的淘寶客登錄功能,結(jié)合實(shí)例形式分析了Django框架基于淘寶接口的登錄功能相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了python,Django實(shí)現(xiàn)的淘寶客登錄功能。分享給大家供大家參考,具體如下:

在整理python,django資料的時候,發(fā)現(xiàn)了這個東西,也許是當(dāng)初某位網(wǎng)友或者朋友發(fā)過來參考或者一起探討修改的東西,現(xiàn)在不記得了,也許taobao的接口都變了也有可能,但總體來說還是有參考價值的,主要是做淘寶客客或者返利網(wǎng)會用到taobao登錄而用的.

參考代碼如下:

#!/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()
      #更新應(yīng)用用戶綁定
      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 認(rèn)證返回值中的簽名
    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 符, 因此需要清除才能驗(yàn)證
    if top_sign == local_sign.strip(): # 清除 \n
      # 創(chuàng)建/返回淘寶賬戶
      tb_user, tb_user_created = taobao_user.objects.get_or_create(id=int(visitor_id),
                     nick=username)
      #建立系統(tǒng)用戶, 用戶名存在則創(chuàng)建隨機(jī)數(shù)擴(kuò)展
      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)
        #如果應(yīng)用賬戶沒有綁定系統(tǒng)用戶,則用戶第一次時可以修改用戶名
        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)

希望本文所述對大家基于Django框架的Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • 在Python的Tornado框架中實(shí)現(xiàn)簡單的在線代理的教程

    在Python的Tornado框架中實(shí)現(xiàn)簡單的在線代理的教程

    這篇文章主要介紹了在Python的Tornado框架中實(shí)現(xiàn)簡單的在線代理的教程,代理功能是一個常見的網(wǎng)絡(luò)編程實(shí)現(xiàn),需要的朋友可以參考下
    2015-05-05
  • Python開根號的幾種方式詳解

    Python開根號的幾種方式詳解

    使用Python中的自帶庫math、自帶函數(shù)pow和自帶庫cmath來對數(shù)字進(jìn)行開根號運(yùn)算,這篇文章主要介紹了Python開根號的幾種方式,需要的朋友可以參考下
    2023-01-01
  • Python實(shí)現(xiàn)隨機(jī)生成有效手機(jī)號碼及身份證功能示例

    Python實(shí)現(xiàn)隨機(jī)生成有效手機(jī)號碼及身份證功能示例

    這篇文章主要介紹了Python實(shí)現(xiàn)隨機(jī)生成有效手機(jī)號碼及身份證功能,結(jié)合完整實(shí)例形式分析了Python基于手機(jī)號與身份證算法實(shí)現(xiàn)隨機(jī)手機(jī)號及身份證的生成功能,涉及Python日期、隨機(jī)數(shù)、文件讀取等相關(guān)操作技巧,需要的朋友可以參考下
    2017-06-06
  • Python從MP3文件獲取id3的方法

    Python從MP3文件獲取id3的方法

    這篇文章主要介紹了Python從MP3文件獲取id3的方法,實(shí)例分析了Python操作文件屬性的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • python實(shí)現(xiàn)二分類的卡方分箱示例

    python實(shí)現(xiàn)二分類的卡方分箱示例

    今天小編就為大家分享一篇python實(shí)現(xiàn)二分類的卡方分箱示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • python區(qū)塊鏈實(shí)現(xiàn)簡版工作量證明

    python區(qū)塊鏈實(shí)現(xiàn)簡版工作量證明

    這篇文章主要為大家介紹了python區(qū)塊鏈實(shí)現(xiàn)簡版工作量證明詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • python3注冊全局熱鍵的實(shí)現(xiàn)

    python3注冊全局熱鍵的實(shí)現(xiàn)

    這篇文章主要介紹了python3注冊全局熱鍵的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • Python利用pdfplumber庫提取pdf中的文字

    Python利用pdfplumber庫提取pdf中的文字

    pdfplumber是一個用于從PDF文檔中提取文本和表格數(shù)據(jù)的Python庫,它可以幫助用戶輕松地從PDF文件中提取有用的信息,例如表格、文本、元數(shù)據(jù)等,本文將給大家介紹如何通過Python的pdfplumber庫提取pdf中的文字,需要的朋友可以參考下
    2023-05-05
  • Scrapy爬蟲文件批量運(yùn)行的實(shí)現(xiàn)

    Scrapy爬蟲文件批量運(yùn)行的實(shí)現(xiàn)

    這篇文章主要介紹了Scrapy爬蟲文件批量運(yùn)行的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • python抓取京東商城手機(jī)列表url實(shí)例代碼

    python抓取京東商城手機(jī)列表url實(shí)例代碼

    python抓取京東商城手機(jī)列表url實(shí)例分享,大家參考使用吧
    2013-12-12

最新評論