def post(self, request, *args, **kwargs): logger = getLogger('django.request.WechatAuthSuccessPageView') auth_code = request.data['auth_code'] component = get_component() # 拿到授权公众号的信息 result = component.query_auth(auth_code) authorizer_appid = result['authorization_info']['authorizer_appid'] expires_in = result['authorization_info']['expires_in'] access_token_key = wechat_caches.CACHE_WECHAT_ACCESS_CODE.format(authorizer_appid) refresh_token_key = wechat_caches.CACHE_WECHAT_REFRESH_CODE.format(authorizer_appid) app_info = component.get_authorizer_info(authorizer_appid) if not Wechat.objects.filter(appid=authorizer_appid).exists(): wechat = Wechat.objects.create_from_api_result(app_info) # wechat.owner = request.user # wechat.save() else: wechat = Wechat.objects.get(appid=authorizer_appid) if not wechat.authorized: wechat.authorized = True wechat.save() caches['wechat'].set( access_token_key, result['authorization_info']['authorizer_access_token'], expires_in ) caches['wechat'].set( refresh_token_key, result['authorization_info']['authorizer_refresh_token'], expires_in ) return HttpResponse('success')
def post(self, request, *args, **kwargs): """ 处理微信服务器提交的数据 """ logger = getLogger('django.request.AuthEventProcessView') message = self.preprocess_message(request) logger.info('收到事件:{0}'.format(message)) component = get_component() # 推送component_verify_ticket协议 if message['InfoType'].lower() == 'component_verify_ticket': component.cache_component_verify_ticket( request.body, request.query_params['msg_signature'], request.query_params['timestamp'], request.query_params['nonce'] ) logger.info('成功获取component_verify_ticket') return HttpResponse('success') # 取消授权通知 elif message['InfoType'].lower() == 'unauthorized': authorizer_appid = message['AuthorizerAppid'] try: wechat = Wechat.objects.get(appid=authorizer_appid) except Wechat.DoesNotExist: return HttpResponse('success') wechat.authorized = False wechat.save() return HttpResponse('success') else: pass
def post(self, request, *args, **kwargs): """ 处理微信服务器提交的数据 """ logger = getLogger('django.request.AuthEventProcessView') message = self.preprocess_message(request) logger.info('收到事件:{0}'.format(message)) component = get_component() # 推送component_verify_ticket协议 if message['InfoType'].lower() == 'component_verify_ticket': component.cache_component_verify_ticket( request.body, request.query_params['msg_signature'], request.query_params['timestamp'], request.query_params['nonce'] ) logger.info('成功获取component_verify_ticket') return HttpResponse('success') # 取消授权通知 elif message['InfoType'].lower() == 'unauthorized': authorizer_appid = message['AuthorizerAppid'] try: wechat = Wechat.objects.get(appid=authorizer_appid) except Wechat.DoesNotExist: return HttpResponse('success') wechat.authorized = False wechat.save() return HttpResponse('success') else: pass
def post(self, request, *args, **kwargs): logger = getLogger('django.request.WechatAuthSuccessPageView') auth_code = request.data['auth_code'] component = get_component() # 拿到授权公众号的信息 result = component.query_auth(auth_code) authorizer_appid = result['authorization_info']['authorizer_appid'] expires_in = result['authorization_info']['expires_in'] access_token_key = wechat_caches.CACHE_WECHAT_ACCESS_CODE.format(authorizer_appid) refresh_token_key = wechat_caches.CACHE_WECHAT_REFRESH_CODE.format(authorizer_appid) app_info = component.get_authorizer_info(authorizer_appid) if not Wechat.objects.filter(appid=authorizer_appid).exists(): wechat = Wechat.objects.create_from_api_result(app_info) # wechat.owner = request.user # wechat.save() else: wechat = Wechat.objects.get(appid=authorizer_appid) if not wechat.authorized: wechat.authorized = True wechat.save() caches['wechat'].set( access_token_key, result['authorization_info']['authorizer_access_token'], expires_in ) caches['wechat'].set( refresh_token_key, result['authorization_info']['authorizer_refresh_token'], expires_in ) return HttpResponse('success')
def get(self, request, *args, **kwargs): logger = getLogger('django.request.WechatAuthPageView') component = get_component() result = component.create_preauthcode() auth_url = settings.AUTH_URL.format( component_appid=settings.COMPONENT_APP_ID, pre_auth_code=result['pre_auth_code'], redirect_uri=settings.AUTH_REDIRECT_URI) return Response({'auth_url': auth_url})
def process_wechat_query_auth_code_test(FromUserName, query_auth_code): """ 处理发布前微信的自动化测试query_auth_code """ logger = get_task_logger('process_wechat_query_auth_code_test') logger.info(FromUserName) logger.info(query_auth_code) component = get_component() client = component.get_client_by_authorization_code(query_auth_code) client.message.send_text(FromUserName, query_auth_code + '_from_api')
def process_wechat_query_auth_code_test(FromUserName, query_auth_code): """ 处理发布前微信的自动化测试query_auth_code """ logger = get_task_logger('process_wechat_query_auth_code_test') logger.info(FromUserName) logger.info(query_auth_code) component = get_component() client = component.get_client_by_authorization_code(query_auth_code) client.message.send_text(FromUserName, query_auth_code+'_from_api')
def get(self, request, *args, **kwargs): logger = getLogger('django.request.WechatAuthPageView') component = get_component() result = component.create_preauthcode() auth_url = settings.AUTH_URL.format( component_appid=settings.COMPONENT_APP_ID, pre_auth_code=result['pre_auth_code'], redirect_uri=settings.AUTH_REDIRECT_URI ) return Response({'auth_url': auth_url})
def preprocess_message(self, request): component = get_component() content = component.crypto.decrypt_message( request.body, request.query_params['msg_signature'], int(request.query_params['timestamp']), int(request.query_params['nonce'])) message = xmltodict.parse(to_text(content))['xml'] cc = json.loads(json.dumps(message)) cc['CreateTime'] = int(cc['CreateTime']) cc['CreateTime'] = datetime.fromtimestamp(cc['CreateTime']) if 'MsgId' in cc: cc['MsgId'] = int(cc['MsgId']) return cc
def preprocess_message(self, request): component = get_component() content = component.crypto.decrypt_message( request.body, request.query_params['msg_signature'], int(request.query_params['timestamp']), int(request.query_params['nonce']) ) message = xmltodict.parse(to_text(content))['xml'] cc = json.loads(json.dumps(message)) cc['CreateTime'] = int(cc['CreateTime']) cc['CreateTime'] = datetime.fromtimestamp(cc['CreateTime']) if 'MsgId' in cc: cc['MsgId'] = int(cc['MsgId']) return cc
def client(self): component = get_component() return component.get_client_by_appid(self.appid)
def client(self): component = get_component() return component.get_client_by_appid(self.appid)