def get(self, request): code = request.GET.get('code') token_code = OAuthQQ(client_id=settings.QQ_CLIENT_ID, client_secret=settings.QQ_CLIENT_SECRET, redirect_uri=settings.QQ_REDIRECT_URI) try: acess_token = token_code.get_access_token(code) openid = token_code.get_open_id(acess_token) print(openid) except Exception as ret: return ret try: oauth_user = OAuthQQUser.objects.get(openid=openid) except OAuthQQUser.DoesNotExist: # openid 需要加密 token = get_acess_token(openid) # 将加密的唯一表示openid 响应给前端 return render(request, 'oauth_callback.html', {'openid': token}) else: user = oauth_user.user login(request, user) next = request.GET.get('state') response = redirect(next or '/') response.set_cookie('username', user.username, max_age=settings.SESSION_COOKIE_AGE) # 合并购物车 merge_carts_cookie_2_redis(request, response) return response
def get(self, request): code = request.GET.get('code') if code is None: return JsonResponse({'code': 400, 'errmsg': "没有code参数"}) # QQ登录参数 # 我们申请的 客户端id QQ_CLIENT_ID = '101474184' # 我们申请的 客户端秘钥 QQ_CLIENT_SECRET = 'c6ce949e04e12ecc909ae6a8b09b637c' # 我们申请时添加的: 登录成功后回调的路径 QQ_REDIRECT_URI = 'http://www.meiduo.site:8080/oauth_callback.html' qq = OAuthQQ(client_id=QQ_CLIENT_ID, client_secret=QQ_CLIENT_SECRET, redirect_uri=QQ_REDIRECT_URI) # 使用code换取token access_token = qq.get_access_token(code) # 使用token换取openid openid = qq.get_open_id(access_token) from apps.oauth.models import OAuthQQUser # 根据openid进行判断 try: qquser = OAuthQQUser.objects.get(openid=openid) except: from apps.oauth.utils import generate_access_token token = generate_access_token(openid) return JsonResponse({'code': 300, 'access_token': token}) else: from django.contrib.auth import login login(request, qquser.user) response = JsonResponse({'code': 0, 'errmsg': "OK"}) response.set_cookie('username', qquser.user.username, max_age=14 * 24 * 3600) return response
def get(self,request): # 1.获取code值和state code=request.GET.get("code") state=request.GET.get("state") print(code,state) state="/index/" if code is None or state is None: return JsonResponse({"error":"缺少参数"}) #2.生成qq对象 qq=OAuthQQ(client_id=settings.QQ_CLIENT_ID,client_secret=settings.QQ_CLIENT_SECRET, redirect_uri=settings.QQ_REDIRECT_URI,state=state) try: # 3.调用方法获取access_token值 access_token=qq.get_access_token(code) openid=qq.get_open_id(access_token) except: return JsonResponse({"error":"网络错误"}) try: #判断qq有没有绑定美多账号 qq_user=OAuthQQUser.objects.get(openid=openid) except: tjw = TJW(settings.SECRET_KEY, 300) openid = tjw.dumps({'openid': openid}).decode() return render(request, 'oauth_callback.html', {'token': openid}) #绑定的用户 login(request,qq_user.user) #将用户写入cookie方便在页面中展示 response=redirect(state) response.set_cookie("username",qq_user.user.username,60 * 60 * 2) #合并购物车 response = merge_cart_cookie_to_redis(request,qq_user.user,response) return response
def get(self, request): # 去查看前端的qq登陆的js,然后搞一搞urls # 提取前端的查询参数:记录用户从哪里去到login界面 next = request.GET.get('next', '/') QQ_CLIENT_ID = '101518219' QQ_CLIENT_SECRET = '418d84ebdc7241efb79536886ae95224' QQ_REDIRECT_URI = 'http://www.meiduo.site:8000/oauth_callback' # 拼接qq登陆的路径 oauth = OAuthQQ(QQ_CLIENT_ID, QQ_CLIENT_SECRET, QQ_REDIRECT_URI, state=next) # https://graph.qq.com/oauth2.0/authorize?response_type = code & client_id = 123 & redirect_uri = xxx & state = next # 到qq互联的文档中查看拼接的路径 # login_url = .... # 使用QQtool 查看里面的qq_url 看看怎么拼接的 # oauth = OAuthQQ(client_id='appid', client_secret='appkey', redirect_uri='回调地址', state='记录状态') # 讲义上的那几个id啥的复制过来 # 然后实例化oauth对象 login_url = oauth.get_qq_url() return http.JsonResponse({ 'login_url': login_url, 'code': RETCODE.OK, 'errmsg': 'OK' })
def get(self, request): code = request.GET.get('code') if not code: return http.JsonResponse({'code': 400, 'errmsg': '缺少code参数'}) oauth = OAuthQQ(client_id=settings.QQ_CLIENT_ID, client_secret=settings.QQ_CLIENT_SECRET, redirect_uri=settings.QQ_REDIRECT_URI) try: access_token = oauth.get_access_token(code) openid = oauth.get_open_id(access_token) except Exception as e: logger.error(e) return http.JsonResponse({'code': 400, 'errmsg': '获取openid出错'}) try: oauth_qq = OAuthQQUser.objects.get(openid=openid) except Exception as e: # 采用session形式保存openid # 创建request.session['键'] = 值 # 取值value = request.session.get('键',默认值) # 设置时长request.session.set_expiry(value) request.session['openid'] = openid request.session.set_expiry(600) return http.JsonResponse({'code': 300, 'errmsg': 'ok'}) else: user = oauth_qq.user login(request, user) response = http.JsonResponse({'code': 0, 'errmsg': 'ok'}) response.set_cookie('username', user.username, max_age=3600 * 24 * 14) return response
def get(self, request): # 1.获取next(从那里去到login界面)参数路径 next = request.query_params.get('next') if not next: # 如果没有指定来源将来登录成功就回到首页 next = '/' # QQ登录参数 """ QQ_CLIENT_ID = '101514053' QQ_CLIENT_SECRET = '1075e75648566262ea35afa688073012' QQ_REDIRECT_URI = 'http://www.meiduo.site:8080/oauth_callback.html' oauthqq = OAuthQQ(client_id='101514053', client_secret='1075e75648566262ea35afa688073012', redirect_uri='http://www.meiduo.site:8080/oauth_callback.html', state=next) """ # 2.创建QQ登录sdk 的对象 oauthqq = OAuthQQ(client_id=settings.QQ_CLIENT_ID, client_secret=settings.QQ_CLIENT_SECRET, redirect_uri=settings.QQ_REDIRECT_URI, state=next) # 3.调用它里面的get_qq_url方法来拿到拼接好的扫码链接 login_url = oauthqq.get_qq_url() # 4.把扫码url响应给前端 return Response({'login_url': login_url})
def get(self, request): #获取从前段发送来的code参数 code = request.GET.get("code") #验证code获取token try: oauth_qq = OAuthQQ(client_id=settings.QQ_CLIENT_ID, client_secret=settings.QQ_CLIENT_SECRET, redirect_uri=settings.QQ_REDIRECT_URI) token = oauth_qq.get_access_token(code) #根据token获取openid openid = oauth_qq.get_open_id(access_token=token) except Exception as e: print(e) return JsonResponse({"code": 400, 'errmsg': "qq登录失败!"}) try: oauth_qq = OAuthQQUser.objects.get(openid=openid) except OAuthQQUser.DoesNotExist as e: #用户没有绑定过qq,我们需要返回加密的openid access_token = generate_access_token(openid) return JsonResponse({'access_token': access_token}) #用户已经绑定过qq-->登录成功 user = oauth_qq.user #状态保持 login(request, user) response = JsonResponse({'code': 0, 'errmsg': "ok"}) response.set_cookie("username", user.username, max_age=3600 * 24 * 14) return response
def get(self, request): code = request.GET.get('code') state = request.GET.get('state', '/') oauth = OAuthQQ( client_id=settings.QQ_CLIENT_ID, client_secret=settings.QQ_CLIENT_SECRET, redirect_uri=settings.QQ_REDIRECT_URI, ) try: access_token = oauth.get_access_token(code) openid = oauth.get_open_id(access_token) except Exception as e: logger.error(e) return JsonResponse({ 'code': RETCODE.SERVERERR, 'errmsg': '服务器发生错误' }) try: oauth_model = OauthQQUser.objects.get(openid=openid) except OauthQQUser.DoesNotExist: # 没有查到openid, 说明是新用户,先把openid保存在前端的隐藏标签中 openid = save_openid(openid) context = {"openid": openid} return render(request, 'oauth_callback.html', context) else: user = oauth_model.user login(request, user) response = redirect(state) response.set_cookie('username', user.username, max_age=settings.SESSION_COOKIE_AGE) return response
def get(self, request): code = request.GET.get('code') state = request.GET.get('state') # redirect_uri = request.GET.get('redirect_uri') if code == None: return HttpResponseBadRequest('code为空!') oauth = OAuthQQ(client_id=QQ_CLIENT_ID, client_secret=QQ_CLIENT_SECRET, redirect_uri=QQ_REDIRECT_URI, state=next) token = oauth.get_access_token(code) openid = oauth.get_open_id(token) try: qquser = OAuthQQUser.objects.get(openid=openid) except OAuthQQUser.DoesNotExist: return render(request, 'oauth_callback.html', context={'openid': openid}) else: login(request, qquser.user) response = redirect(reverse('user1:index')) # 设置cookie response.set_cookie('username', qquser.user.username, max_age=24 * 3600) return response
def get(self, request): code = request.GET.get("code") state = request.GET.get("state") if code is None: return HttpResponseBadRequest("meiyou1code") # 导入QQ互联包 from QQLoginTool.QQtool import OAuthQQ oauth = OAuthQQ(client_id=settings.QQ_CLIENT_ID, client_secret=settings.QQ_CLIENT_SECRET, redirect_uri=settings.QQ_REDIRECT_URI, state=state) # 获取token token = oauth.get_access_token(code) # 获取openid openid = oauth.get_open_id(token) # 判断openid try: qquser = OAuthQQUser.objects.get(openid=openid) except OAuthQQUser.DoesNotExist: return render(request, "oauth_callback.html", context={"openid": openid}) else: login(request, qquser.user) response = redirect(reverse("concents:index")) response.set_cookie("username", qquser.user, max_age=3600 * 24) return response
def get(self, request): code = request.GET.get('code') if code is None: return http.HttpResponseForbidden('必传参数不能为空') # 根据code 获取access_token auth = OAuthQQ( client_id=settings.QQ_CLIENT_ID, client_secret=settings.QQ_CLIENT_SECRET, redirect_uri=settings.QQ_REDIRECT_URI, ) try: access_token = auth.get_access_token(code) # 根据access_token 获取openid openid = auth.get_open_id(access_token) except: return http.HttpResponseForbidden('获取openid失败') try: user = OAuthQQUser.objects.get(openId=openid) except BaseException as e: # 用户首次用QQ 登录 access_token = MD5(openid).generate_access_token() context = {'access_token': access_token} return render(request, 'oauth_callback.html', context) else: # 用户非首次QQ登录 # 1 状态保持 login(request, user.user) response = redirect(reverse('contents:index')) response.set_cookie('username', user.user.username, max_age=3600 * 24 * 7) return response
def get(self, request): code = request.GET.get('code') if not code: return http.HttpResponseForbidden("缺少code") oauth = OAuthQQ(client_id=settings.QQ_CLIENT_ID, client_secret=settings.QQ_CLIENT_SECRET, redirect_uri=settings.QQ_REDIRECT_URI) try: aaaaaccess_token = oauth.get_access_token(code) openid = oauth.get_open_id(aaaaaccess_token) except Exception as e: logger.error(e) return http.HttpResponseServerError('OAuth2.0认证失败') try: oauth_user = OauthQQUser.objects.get(openid=openid) except OauthQQUser.DoesNotExist: # 没有帮顶qq access_token = generate_access_token(openid) context = {'access_token': access_token} return render(request, 'oauth_callback.html', context) else: qq_user = oauth_user.user login(request, qq_user) response = redirect(reverse('contents:index')) response.set_cookie('username', qq_user.username, max_age=3600 * 24 * 14) return response
def get(self, request): #1 获取参数 code = request.GET.get('code') #2 校验参数 if not code: return http.HttpResponseForbidden("code丢失") # 3 获取access_token oauth_qq = OAuthQQ(client_id=settings.QQ_CLIENT_ID, client_secret=settings.QQ_CLIENT_SECRET, redirect_uri=settings.QQ_REDIRECT_URI, state='/') access_token = oauth_qq.get_access_token(code=code) # 4 获取openid openid = oauth_qq.get_open_id(access_token=access_token) #5 判断是否绑定 try: qq_user = OAuthQQUser.objects.get(openid=openid) except Exception as e: print("没有该用户") #初次授权 encrypt_openid = encode_openid(openid) context = {"token": encrypt_openid} return render(request, 'oauth_callback.html', context=context) else: # 非初次授权,获取梅朵用户 user = qq_user.user login(request, user) response = redirect(reverse('contents:index')) response.set_cookie('username', user.username) response = merge_cart_cookie_to_redis(request, user, response) return response
def get(self, request): """Oauth2.0认证""" # 接收Authorization Code code = request.GET.get('code') if not code: return http.HttpResponseForbidden('缺少code') # 创建工具对象 oauth = OAuthQQ(client_id=settings.QQ_CLIENT_ID, client_secret=settings.QQ_CLIENT_SECRET, redirect_uri=settings.QQ_REDIRECT_URI) try: # 携带 code 向 QQ服务器 请求 access_token access_token = oauth.get_access_token(code) # 携带 access_token 向 QQ服务器 请求 openid openid = oauth.get_open_id(access_token) except Exception as e: # 如果上面获取 openid 出错, 则验证失败 logger.error(e) # 返回结果 return http.HttpResponseServerError('OAuth2.0认证失败') try: oauth_user = OAuthQQUser.objects.get(openid=openid) except OAuthQQUser.DoesNotExist: # 如果 openid 没绑定美多商城用户,进入这里: # 调用我们封装好的方法, 对 openid 进行加密, 生成 access_token 字符串 access_token = generate_access_token(openid) # 拿到 access_token 字符串后, 拼接字典 context = {'access_token': access_token} # 返回响应, 重新渲染 return render(request,'oauth_callback.html', context) else: # 如果 openid 已绑定美多商城用户 # 根据 user 外键, 获取对应的 QQ用户 qq_user = oauth_user.user # 实现状态保持 login(request, qq_user) # 创建重定向到主页的对象 response = redirect(reverse('contents:index')) # 将用户信息写入到 cookie 中,有效期15天 response.set_cookie('username',qq_user.username,max_age=3600 * 24 * 15) # 合并购物车 response = merge_cart_cookie_to_redis(request=request, user=qq_user, response=response) # 返回响应 return response
def get(self,request): # next 表示从哪个页面进入到的登录页面,将来登录成功后,就自动回到那个页面 next = request.GET.get('enxt') # 获取 QQ 登录页面网址 # 创建 OAuthQQ 类的对象 oauth = OAuthQQ(client_id=settings.QQ_CLIENT_ID, client_secret=settings.QQ_CLIENT_SECRET, redirect_uri=settings.QQ_REDIRECT_URI, state=next) # 调用对象的获取 qq 地址方法 login_url = oauth.get_qq_url() # 返回登陆地址 return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK', 'login_url':login_url})