def after_submit(self): # Redirect the user to the confirmation page for list_id in self.resubscribed_lists: list = CampaignMonitorList.get_instance(list_id=list_id) if (list and list.confirmation_page and list.confirmation_page.is_accessible()): redirect(list.confirmation_page.get_uri())
def step1(self, code=None, target_url=None, **kwargs): if target_url: self.target_url = target_url session[SESSION_PREFIX + "target_url"] = target_url flow = OAuth2WebServerFlow(self.provider.client_id, self.provider.client_secret, self.provider.scope, redirect_uri=self.step_url(1)) flow.params["access_type"] = self.provider.access_type if not code: redirect(flow.step1_get_authorize_url()) if self.provider.debug_mode: print(styled("Google authorization code:", "magenta"), code) credentials = flow.step2_exchange(code) session[SESSION_PREFIX + "credentials"] = credentials if self.provider.debug_mode: print(styled("Google refresh token:", "magenta"), end=' ') print(credentials.refresh_token) print(styled("Google access token:", "magenta"), end=' ') print(credentials.access_token) redirect(self.step_url(2))
def check_step1_errors(self, **kwargs): if "error" in kwargs: error_reason = kwargs.get('error_reason', 'Error') if error_reason == 'user_denied': redirect(self.target_url) else: raise FacebookOAuthException(error_reason)
def check_step2_errors(self, result): if 'error' in result: if 'OAuthException' in result['error']: redirect(self.step_url(1)) else: msg = result.get('error_user_msg', 'Facebook user ' 'authentification error') raise FacebookOAuthException(msg)
def after_submit(self): # Redirect the user to the confirmation success page of the first # list if the subscriber is already subscribed to all lists if not self.is_new: for list in self.subscribed_lists: if (list.confirmation_success_page and list.confirmation_success_page.is_accessible()): redirect(list.confirmation_success_page.get_uri()) # Redirect the user to the confirmation page for list in self.subscribed_lists: if (list.confirmation_page and list.confirmation_page.is_accessible()): redirect(list.confirmation_page.get_uri())
def step1(self, code=None, target_url=None, **kwargs): self.check_step1_errors(**kwargs) if target_url: self.target_url = target_url session[SESSION_PREFIX + "target_url"] = target_url if not code: params = { 'client_id': self.provider.client_id, 'redirect_uri': self.step_url(1), 'scope': ','.join(self.provider.scope) } login_uri = 'https://www.facebook.com/dialog/oauth?' + \ urllib.parse.urlencode(params) redirect(login_uri) if self.provider.debug_mode: print(styled("Facebook authorization code:", "magenta"), code) params = { 'client_id': self.provider.client_id, 'redirect_uri': self.step_url(1), 'client_secret': self.provider.client_secret, 'code': code } token_uri = 'https://graph.facebook.com/v2.3/oauth/access_token?' \ + urllib.parse.urlencode(params) json_file = urllib.request.urlopen(token_uri).readline() token_data = json.loads(json_file) if not token_data.get("access_token"): raise FacebookOAuthBadResponseException( token_data, "Expected an 'access_token' key") session[SESSION_PREFIX + "credentials"] = token_data if self.provider.debug_mode: print(styled("Facebook token data:", "magenta"), end=' ') print(token_data) redirect(self.step_url(2))
def step2(self): credentials = session.get(SESSION_PREFIX + "credentials") if not credentials or credentials.access_token_expired: redirect(self.step_url(1)) http_auth = credentials.authorize(httplib2.Http()) oauth2_service = discovery.build('oauth2', 'v2', http_auth) user_data = oauth2_service.userinfo().get().execute() if self.provider.debug_mode: print(styled("Google user profile:", "magenta"), user_data) self.provider.login(user_data) del session[SESSION_PREFIX + "credentials"] redirect(self.target_url)
def step2(self): credentials = session.get(SESSION_PREFIX + "credentials") if not credentials: redirect(self.step_url(1)) fields = ['name', 'email'] query = '{}{}{}{}'.format( 'https://graph.facebook.com', '/me?', 'fields=' + ','.join(fields), '&access_token=' + credentials['access_token']) user_data_file = urllib.request.urlopen(query).readline() user_data = json.loads(user_data_file) self.check_step2_errors(user_data) if self.provider.debug_mode: print(styled("Facebook user profile:", "magenta"), user_data) self.provider.login(user_data) del session[SESSION_PREFIX + "credentials"] redirect(self.target_url)