def process_request(self, request): if not runtime_settings.get(TMWEIXIN_TURN_ON): return assert hasattr(request, 'session'), (u"session should be available" u"please add session middleware in settings") exclude_path = runtime_settings.get(PATH_EXCLUDE_LIST_TOKEN) or [] for p in exclude_path: p = re.compile(p) if p.match(request.get_full_path()): return openid = request.session.get("openid", None) assert openid if hasattr(request, 'wx_user'): return try: request.wx_user = User.objects.get(openid=openid) except User.DoesNotExist: pass else: return if request.GET.get('state') == "reqwebauth": # user auth success. get weixin request with state of reqwebauth code = request.GET.get('code') if code: actken = OAuth2(code).get_data().get("access_token") if actken is None: return HttpResponseServerError() # actken may get error wxu = UserInfo(openid, actken).get_data() if wxu.get('errcode'): return HttpResponseServerError() else: # if user is not subscribed,let user give us permission. # use this permission to get user info wxu = UserInfo(openid).get_data() if wxu.get(u'subscribe') == 0 and request.GET.get('state') != "reqwebauth": url = OAuth2.get_authorize_uri( redirect="http://%s%s" % (settings.SITE_HOST, request.get_full_path()), scope=OAuth2.SNSAPI_USERINFO, state="reqwebauth") return HttpResponseRedirect(url) wxu_headimgurl = wxu.get('headimgurl') if wxu_headimgurl and wxu_headimgurl.endswith(r'/0'): wxu_headimgurl = wxu_headimgurl[:-2] + "/132" else: wxu_headimgurl = "" m = User(openid=wxu.get('openid'), nickname=wxu.get('nickname'), sex=int(wxu.get('sex')), city=wxu.get('city'), province=wxu.get('province'), headimgurl=wxu_headimgurl) m.save() request.wx_user = m
def sns_user_info(request): url = OAuth2.get_authorize_uri(scope=OAuth2.SNSAPI_USERINFO, redirect="http://%s%s" % (host, reverse_lazy("tmtest:auth", args=()))) print(url) return HttpResponseRedirect(redirect_to=url)
def sns_base(request): url = OAuth2.get_authorize_uri(redirect="http://%s%s" % (host, reverse_lazy("tmtest:auth", args=()))) print(url) return HttpResponseRedirect(redirect_to=url)